Click here to Skip to main content
15,885,914 members
Articles / Containers / Virtual Machine
Article

C# Code profiling

Rate me:
Please Sign up or sign in to vote.
1.24/5 (13 votes)
24 Aug 20071 min read 55.6K   11   14
Optimize your code

Introduction

Video games programmer have always the same objective : write fastest code as possible. There's no need to use assembly everywhere or know MSIL by heart, just use a profiler and see ;p Loop is something very usefull and very used in C# but what is the best solution for an ArrayList to browse all items ? Is IEnumerator faster than for () ? Bets are opened. For this article, I've chosen Red Gate Ants Profiler 2.6.0 Build 62 (http://www.red-gate.com/products/ants_profiler/index.htm). Results are... suprising.

ArrayList

I've found 4 ways to browse an ArrayList and Antony another one :

C#
static public void Browse1 (ArrayList list)
{
 int Total = 0;
 for (int i = 0; i < list.Count; i++)
  {
   Total += (int)list[i];
  }
}

static public void Browse2 (ArrayList list)
{
 int Total = 0;
 int Count = list.Count;
 for (int i = 0; i < Count; i++)
 {
  Total += (int)list[i];
 }
}

static public void Browse3 (ArrayList list)
{
 int Total = 0;
 IEnumerator Iterator = list.GetEnumerator (); 
 while (Iterator.MoveNext ())
 {
  Total += (int)Iterator.Current;
 }
}

static public void Browse4 (ArrayList list)
{
 int Total = 0;
 foreach (int i in list)
 {
  Total += i;
 }
}

static public void Browse5(ArrayList list)
{
 int Total = 0;
 for (int i = list.Count - 1; i >= 0; i--)
 {
  Total += (int)list[i];
 }
}

In Main(), just fill the ArrayList :

C#
ArrayList MyList = new ArrayList ();

// Add items
DateTime CurrentTime = DateTime.Now;
Random rand = new Random (CurrentTime.Millisecond);
int Count = (int)(CurrentTime.Ticks % (1<<25));
int NewValue = (rand.Next () % 100);

Console.WriteLine ("Fill list with {0} elements", Count);
for (int i = 0; i < Count; i++)
{
    MyList.Add (NewValue);
}

// Browse
DateTime d1, d2, d3, d4, d5, d6;
d1 = DateTime.Now;
Console.WriteLine ("Browse1");
Browse1 (MyList);
d2 = DateTime.Now;
Console.WriteLine ("Browse2");
Browse2 (MyList);
d3 = DateTime.Now;
Console.WriteLine ("Browse3");
Browse3 (MyList);
d4 = DateTime.Now;
Console.WriteLine ("Browse4");
Browse4 (MyList);
d5 = DateTime.Now;
Console.WriteLine("Browse5");
Browse5(MyList);
d6 = DateTime.Now;

// Print results
TimeSpan r1,r2,r3,r4,r5;
r1 = d2 - d1;
r2 = d3 - d2;
r3 = d4 - d3;
r4 = d5 - d4;
r5 = d6 - d5;

Console.WriteLine("Browse1 : {0}", r1.TotalMilliseconds);
Console.WriteLine("Browse2 : {0}", r2.TotalMilliseconds);
Console.WriteLine("Browse3 : {0}", r3.TotalMilliseconds);
Console.WriteLine("Browse4 : {0}", r4.TotalMilliseconds);
Console.WriteLine("Browse5 : {0}", r5.TotalMilliseconds);

Console.WriteLine("Press any key");
Console.ReadKey();

Here are the results (Release Version, PIV C, XP Pro SP2) :

Count
24 145 243
18 491 373
Browse1
327.55 ms
249.56 ms
Browse2
233.96 ms
202.77 ms
Browse3
717.48 ms
545.91 ms
Browse4
717.48 ms
561.51 ms
Browse5
249.56 ms
171.57 ms

Something interesting is in first case, both, Solution 3 & 4 have the same time but not the second one.

Method 2 or 5 seems to be the fastest but not always in the same order.

We can do other tests on other computers:

- Retry with Red Gate Ants Profiler and not only on timer

- Look physical memory allocation to avoid hard disk swap

- Test on virtual machine


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe fastest way Pin
tripnotize10-Aug-11 9:47
tripnotize10-Aug-11 9:47 
AnswerRe: The fastest way Pin
Vincent DUVERNET (Nolmë Informatique)11-Aug-11 11:43
Vincent DUVERNET (Nolmë Informatique)11-Aug-11 11:43 
GeneralMy vote of 1 Pin
HotB16-Jul-10 2:29
HotB16-Jul-10 2:29 
GeneralFaster way (a bit) Pin
AnthonyLloyd3024-Aug-07 6:50
AnthonyLloyd3024-Aug-07 6:50 
GeneralRe: Faster way (a bit) Pin
Vincent DUVERNET (Nolmë Informatique)24-Aug-07 7:54
Vincent DUVERNET (Nolmë Informatique)24-Aug-07 7:54 
GeneralRe: Faster way (a bit) Pin
AnthonyLloyd3024-Aug-07 9:09
AnthonyLloyd3024-Aug-07 9:09 
GeneralRe: Faster way (a bit) Pin
Vincent DUVERNET (Nolmë Informatique)25-Aug-07 4:42
Vincent DUVERNET (Nolmë Informatique)25-Aug-07 4:42 
GeneralRe: Faster way (a bit) Pin
Lunzo22-Nov-07 14:08
Lunzo22-Nov-07 14:08 
GeneralRe: Faster way (a bit) Pin
Vincent DUVERNET (Nolmë Informatique)22-Nov-07 22:15
Vincent DUVERNET (Nolmë Informatique)22-Nov-07 22:15 
GeneralMain method problem Pin
bojan.ni22-Aug-07 12:04
bojan.ni22-Aug-07 12:04 
In Browse1 and Browse2 you use index access which is better because of code optimization. Look at the main method and see that you set Count to deterministic value. Because of that, compiler optimize code and it will implement ArrayList with default capacity set to Count. You would see this if you try to some reverse engineering of exe. Try to use random value and re-test it and you'll see some other results
GeneralRe: Main method problem Pin
Vincent DUVERNET (Nolmë Informatique)23-Aug-07 23:25
Vincent DUVERNET (Nolmë Informatique)23-Aug-07 23:25 
GeneralWrong comparison Pin
brunomsilva16-Aug-06 1:02
brunomsilva16-Aug-06 1:02 
GeneralRe: Wrong comparison Pin
Vincent DUVERNET (Nolmë Informatique)16-Aug-06 2:22
Vincent DUVERNET (Nolmë Informatique)16-Aug-06 2:22 
GeneralRe: Wrong comparison Pin
Rafey21-Feb-07 6:02
Rafey21-Feb-07 6:02 

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.