[Mono-dev] Question about performance improvement in C#

Rusmin Susanto rusminsusanto at yahoo.com
Sun Apr 16 10:18:54 EDT 2006


Hello all.
  I have an optimization question here.
  I have a class that has 2D array of double. This class will be used in scientific iteration.  It need to have operators (+,-, etc.) so that users can do something like this:
   
   A = (B + C + D + E + F)/2.5;
   
  It also has to be fast (as it will be executed thousands of time in iterations and the array dimensions will be large).
   
  When I wrote the application in C# (100%) the performance was bad (about 30 times slower than pure C implementation)
  . 
After profiling I found that the loop inside operator+ that cause problem. We can't have one loop to process all operands because we can only process two at a time (that's what I know. I could be wrong). The creation of temporary object also hurts performance.
   
  The next thing I did to improve performance was to embed the application with C. The array allocation is now done in C. All loops are now executed in C. This approach improves the performance greatly. However, it is still not good enough compared to pure C implementation. It's about 2-3 times slower. After investigating further I found the problem. 
   
  In C, we can do like this:
   
   for(int i = 0; i < rowcount; i++)
 {
  for(int j = 0; j < colcount; i++)
   a[i][j] = (b[i][j] + c[i][j] + d[i][j] + e[i][j] + f[i][j])/2.5;
 }

  In C, we can process all array in one loop. But, in C# we have to process array 2 at a time.  This means (for the above example), we need to execute the loop 4 times instead of once.
   
  What should I do to bring the performance closer to C (of course I wan to keep the class and operators)?
  
How do we tell C# to process all operands at once, instead of only 2 at a time? Do we need to modify the C# language so that it can process all at once? If we do need to modify C# or the JIT compiler, where should I start? 
   
  Many thanks for your attention.
   
   
   
  Rusmin

		
---------------------------------
Blab-away for as little as 1¢/min. Make  PC-to-Phone Calls using Yahoo! Messenger with Voice.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060416/a7176098/attachment.html 


More information about the Mono-devel-list mailing list