Click here to Skip to main content
Licence 
First Posted 16 Aug 2006
Views 31,139
Bookmarked 9 times

C# Code profiling

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 :

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

About the Author

Vincent DUVERNET (Nolmë Informatique)

Web Developer

France France

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThe fastest way Pinmembertripnotize9:47 10 Aug '11  
AnswerRe: The fastest way PinmemberVincent DUVERNET (Nolmë Informatique)11:43 11 Aug '11  
GeneralMy vote of 1 PinmemberHotB2:29 16 Jul '10  
GeneralFaster way (a bit) PinmemberAnthonyLloyd306:50 24 Aug '07  
GeneralRe: Faster way (a bit) PinmemberVincent DUVERNET (Nolmë Informatique)7:54 24 Aug '07  
GeneralRe: Faster way (a bit) PinmemberAnthonyLloyd309:09 24 Aug '07  
GeneralRe: Faster way (a bit) PinmemberVincent DUVERNET (Nolmë Informatique)4:42 25 Aug '07  
GeneralRe: Faster way (a bit) PinmemberLunzo14:08 22 Nov '07  
GeneralRe: Faster way (a bit) PinmemberVincent DUVERNET (Nolmë Informatique)22:15 22 Nov '07  
GeneralMain method problem Pinmemberbojan.ni12:04 22 Aug '07  
GeneralRe: Main method problem PinmemberVincent DUVERNET (Nolmë Informatique)23:25 23 Aug '07  
GeneralWrong comparison Pinmemberbrunomsilva1:02 16 Aug '06  
GeneralRe: Wrong comparison PinmemberVincent DUVERNET (Nolmë Informatique)2:22 16 Aug '06  
GeneralRe: Wrong comparison PinmemberRafey6:02 21 Feb '07  

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.

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