Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > General > News

News Programming news and events.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-20-2008, 12:20 AM
Programming God
 
Join Date: Sep 2006
Posts: 564
Rep Power: 14
Kernel is on a distinguished road
Default An easy and efficient way to improve .NET code performances

Currently, I am getting serious about measuring and improving performances of .NET code. I’ll post more about this topic within the next weeks. For today I would like to present an efficient optimization I found on something we all use massively: loops on collections. I talk here about the cost of the loop itself, i.e for and foreach instructions, not the cost of the code executed in the loop. I basically found that:



  • for loops on List are a bit more than 2 times cheaper than foreach loops on List.
  • Looping on array is around 2 times cheaper than looping on List.
  • As a consequence, looping on array using for is 5 times cheaper than looping on List using foreach (which I believe, is what we all do).


The foreach syntax sugar is a blessing and for many complex loops it is not worth transforming foreach into for. But for certain kind of loops, like short loops made of one or two instructions nested in some other loops, these 5 times factor can become a huge bonus. And the good news is that the code of NDepend has plenty of these nested short loops because of algorithm such as Ranking, Leveland dependencies transitive closure computation.



Here is the code used to benchmark all this:



static void IterateForeachOnList(List list) { foreach(int i in list) { int j = i; } } static void IterateForOnListWithoutCountOptimization(List list) { for(int i=0; i < list.Count; i++) { int j = list [ i ] ; } } static void IterateForOnListWithCountOptimization(List list) { int count = list.Count; for (int i = 0; i < count; i++) { int j = list [ i ] ; } } static void IterateForeachOnArray(int[] array) { foreach (int i in array) { int j = i; } } static void IterateForOnArrayWithoutCountOptimization(int[] array) { for (int i = 0; i < array.Length; i++) { int j = array [ i ] ; } } static void IterateForOnArrayWithCountOptimization(int[] array) { int length = array.Length; for (int i = 0; i < length; i++) { int j = array [ i ] ; } }




Here are the results obtained with the System.Diagnostics.Stopwatch class:









The fact that for is more efficient than foreach results from the fact that foreach is using an enumerator object behind the scene.




The fact that iterating on array is cheaper than iterating on List comes from the fact that the IL has instructions dedicated for array iteration. Here is the Reflector view of the methods:







In the console output, we can see that foreach on array is a tiny bit more costly than for on array. Interestingly enough, I just checked that the C# compiler doesn’t use an enumerator object behind the scene for foreach on array.



Some more interesting finding. The profiler dotTrace v3.1 gives some unrealistic results for this benchmark. IterateForeachOnList is deemed more than 40 times more costly than IterateForOnArray (instead of 5 times). I tested both RecordThreadTime and RecordWallTime mode of the profiler:







Thus I also ran the benchmark with ANTS Profiler v4.1 and got some more realistic results, still not perfect however (IterateForeachOnList is now deemed 8 times more costly than IterateForOnArray,instead of 5 times).







You can download the code of the benchmark here. All tests have been done with release compilation mode.



More...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-20-2008, 02:54 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: An easy and efficient way to improve .NET code performances

Not that .NET code needs any improving in terms of efficiency.
__________________

Quote:
Originally Posted by Jordan View Post
Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:11 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads