Click here to Skip to main content
15,920,030 members
Home / Discussions / C#
   

C#

 
AnswerRe: hi friends Pin
Christian Graus27-Jul-07 21:31
protectorChristian Graus27-Jul-07 21:31 
AnswerRe: hi friends Pin
Guffa27-Jul-07 21:57
Guffa27-Jul-07 21:57 
Questionselect from DataBase Pin
Thaer Hamael27-Jul-07 18:10
Thaer Hamael27-Jul-07 18:10 
AnswerRe: select from DataBase Pin
Christian Graus27-Jul-07 18:24
protectorChristian Graus27-Jul-07 18:24 
AnswerRe: select from DataBase Pin
Paul Conrad27-Jul-07 19:44
professionalPaul Conrad27-Jul-07 19:44 
QuestionSelecte max(Feild)from Pin
Thaer Hamael27-Jul-07 17:58
Thaer Hamael27-Jul-07 17:58 
AnswerRe: Selecte max(Feild)from Pin
Paul Conrad27-Jul-07 19:44
professionalPaul Conrad27-Jul-07 19:44 
QuestionPointers Performance Pin
Leslie Sanford27-Jul-07 16:53
Leslie Sanford27-Jul-07 16:53 
I've used C# for a few years now and have not needed to use its pointers feature. However, I'm in the process of optimizing some code, and I'm looking at pointers as one way to improve performance. The code I'm working with makes extensive use of arrays. Lots and lots of tight loops in which the results are assigned to elements in a array.

I wrote the following console app to compare pointer performance to array performance:

using System;
using System.Diagnostics;
 
namespace PointerTest
{
    class Program
    {
        private const int Size = 10000000;
 
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            float[] data = new float[Size];
 
            unsafe
            {
                fixed(float* p = data)
                {
                    watch.Start();
 
                    for(int i = 0; i < Size; i++)
                    {
                        p[i] = 1.0f;
                    }
 
                    watch.Stop();
 
                    Console.WriteLine("Pointer: " + watch.Elapsed.ToString());
                }
            }
 
            watch.Reset();
            watch.Start();
 
            for(int i = 0; i < Size; i++)
            {
                data[i] = 1.0f;
            }
 
            watch.Stop();
 
            Console.WriteLine("Array: " + watch.Elapsed.ToString());
            Console.Read();
        }
    }
}


The program assigns 10 million items to a pointer pointing to an array and then does the same thing with the array directly. Here are the results:

Pointer: 00:00:00.1147978
Array: 00:00:00.0993352


I ran the program several times, and while the times varied, though not by a great amount, the result was the same: the pointer method is slower.

However, when I used an array size below about 20,000, the pointer method beat out the array method by a small amount.

Any thoughts or insights?
AnswerRe: Pointers Performance Pin
J. Dunlap27-Jul-07 17:37
J. Dunlap27-Jul-07 17:37 
AnswerRe: Pointers Performance Pin
Luc Pattyn27-Jul-07 17:50
sitebuilderLuc Pattyn27-Jul-07 17:50 
GeneralRe: Pointers Performance Pin
Leslie Sanford27-Jul-07 18:39
Leslie Sanford27-Jul-07 18:39 
AnswerRe: Pointers Performance Pin
Guffa27-Jul-07 22:13
Guffa27-Jul-07 22:13 
GeneralRe: Pointers Performance Pin
Leslie Sanford28-Jul-07 5:27
Leslie Sanford28-Jul-07 5:27 
GeneralRe: Pointers Performance [modified] Pin
Robert Surtees28-Jul-07 6:34
Robert Surtees28-Jul-07 6:34 
GeneralRe: Pointers Performance Pin
Leslie Sanford28-Jul-07 7:55
Leslie Sanford28-Jul-07 7:55 
GeneralRe: Pointers Performance Pin
Guffa28-Jul-07 9:33
Guffa28-Jul-07 9:33 
GeneralRe: Pointers Performance Pin
Robert Surtees28-Jul-07 10:58
Robert Surtees28-Jul-07 10:58 
AnswerRe: Pointers Performance Pin
Guffa28-Jul-07 13:37
Guffa28-Jul-07 13:37 
AnswerMessage formatting question (expires on sunday) Pin
Luc Pattyn27-Jul-07 23:34
sitebuilderLuc Pattyn27-Jul-07 23:34 
GeneralRe: Message formatting question (expires on sunday) Pin
Leslie Sanford28-Jul-07 5:35
Leslie Sanford28-Jul-07 5:35 
GeneralRe: Message formatting question (expires on sunday) Pin
Luc Pattyn28-Jul-07 5:44
sitebuilderLuc Pattyn28-Jul-07 5:44 
GeneralRe: Message formatting question (expires on sunday) Pin
Leslie Sanford28-Jul-07 5:46
Leslie Sanford28-Jul-07 5:46 
GeneralRe: Message formatting question (expires on sunday) Pin
Luc Pattyn28-Jul-07 5:54
sitebuilderLuc Pattyn28-Jul-07 5:54 
GeneralRe: Message formatting question (expires on sunday) Pin
Leslie Sanford28-Jul-07 6:18
Leslie Sanford28-Jul-07 6:18 
GeneralRe: Message formatting question (expires on sunday) Pin
Luc Pattyn28-Jul-07 7:15
sitebuilderLuc Pattyn28-Jul-07 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.