Click here to Skip to main content
Click here to Skip to main content

C# Code profiling

, 24 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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 Poke tongue | ;-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 :

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 :

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

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionThe fastest way Pinmembertripnotize10-Aug-11 9:47 
AnswerRe: The fastest way PinmemberVincent DUVERNET (Nolmë Informatique)11-Aug-11 11:43 
GeneralMy vote of 1 PinmemberHotB16-Jul-10 2:29 
GeneralFaster way (a bit) PinmemberAnthonyLloyd3024-Aug-07 6:50 
GeneralRe: Faster way (a bit) PinmemberVincent DUVERNET (Nolmë Informatique)24-Aug-07 7:54 
GeneralRe: Faster way (a bit) PinmemberAnthonyLloyd3024-Aug-07 9:09 
GeneralRe: Faster way (a bit) PinmemberVincent DUVERNET (Nolmë Informatique)25-Aug-07 4:42 
GeneralRe: Faster way (a bit) PinmemberLunzo22-Nov-07 14:08 
GeneralRe: Faster way (a bit) PinmemberVincent DUVERNET (Nolmë Informatique)22-Nov-07 22:15 
GeneralMain method problem Pinmemberbojan.ni22-Aug-07 12:04 
GeneralRe: Main method problem PinmemberVincent DUVERNET (Nolmë Informatique)23-Aug-07 23:25 
GeneralWrong comparison Pinmemberbrunomsilva16-Aug-06 1:02 
GeneralRe: Wrong comparison PinmemberVincent DUVERNET (Nolmë Informatique)16-Aug-06 2:22 
GeneralRe: Wrong comparison PinmemberRafey21-Feb-07 6:02 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 24 Aug 2007
Article Copyright 2006 by Vincent DUVERNET (Nolmë Informatique)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid