[Mono-list] Performance / array access

Tom Fransen t.fransen@mailned.nl
Tue, 14 Jan 2003 22:38:34 +0100


can you spend a few more words on this. Why is the second case slower?

Furthermore if I use a simple loop to fill an array the
speed difference is a factor 3. I have a bubble sort method
that I execute a larger number of times. On Mono it takes 19 seconds
on the MS stuff ~6 seconds. This is a big difference. So why I am
losing a factor 3 on the following loop.

                // Fill aray worst case, all elements need to be swapped
                for(int i=0; i< size; i++)
                    array[i] = size - i;

I am testing with some small benchmarks, but real applications (e.g. an MP3
often use tables stored in arrays to do certain calculations.
So although the benchmark maybe syntetic certain application will
suffer from these penalties.


-----Original Message-----
From: Miguel de Icaza [mailto:miguel@ximian.com]
Sent: Monday, January 13, 2003 3:22 AM
To: Piers Haken
Cc: Tom Fransen; Mono-list@ximian.com
Subject: RE: [Mono-list] Performance / array access


> Yeah, Microsoft's JIT lifts invariant bounds-checks. But I believe
> it's pretty limited.
> For example, the check is removed in the following case:
>   for (int i = 0; i < array.Length; ++i)
>     sum += array [i];
> But not here:
>   int len = array.Length;
>   for (int i = 0; i < len; ++i)
>     sum += array [i];
> So the first case is (counter-intuitively) faster than the second.
> I don't believe Mono's JIT makes this optimization. Maybe the new JIT
> will ;-)

This is a very good observation.  Because it seems that this particular
kind of loop is detected by the JIT engine.

Array-bounds-check elimination is something we want to do with the new
JIT, but it is not implemented at this point.  The new JIT features a
new intermediate representation that simplifies implementing this sort
of thing, but it is still on its bootstrapping phases of life.