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

Alex Chudnovsky alexc at majestic12.co.uk
Sun Apr 16 10:48:06 EDT 2006


Rusmin Susanto wrote:

> It also has to be fast (as it will be executed thousands of time in 
> iterations and the array dimensions will be large).

Its either fast or convinient or some balance between them. In this case 
you seem to want performance, which means you will have to give up 
convinience: in this case you should try not to create class that will 
just hold 2D array - using function calls to get access to one value is 
not exactly performance friendly.

You also use jagged arrays - if you have fixed number of rows and 
columns, then its efficient to access it like you do, say here is faster 
version:

 for(int i = 0; i<rowcount*colcount; i++)
 {
   a[i] = (b[i] + c[i] + d[i] + e[i] + f[i])/2.5; // <--- consider 
getting rid of floating point math, its not as fast as integers
 }

This avoids 2nd loop as well as less calculations to access value, this 
is of course based on the fact that you access your array from top to 
bottom, left to right.

Alex



More information about the Mono-devel-list mailing list