Groups | Blog | Home
all groups > dotnet performance > february 2005 >

dotnet performance : Constants slower than array properties?


WXS
2/14/2005 8:27:02 PM
In the code below the for loop using items.Length seems to run faster the
majority of the time compared the the for loop using the constant MAX_ITEMS.
Why? I'm pretty sure in C++ this wouldn't be the case. When comparing
assembly code the C++ compiler optimized that compare down to about 4 code
bytes including the increment of the loop variable where it is about about 10
code bytes for the constant version in .NET and about 8 or 9 code bytes in
the property version.
When doing several runs it is not consistent in timing but generally
constant comes out slower.. presumably an assembly instruction being used
takes longer for that variation than what is used for properties.

Is this corrected in .NET 2.0? Is this a bad JIT? This seems bad as it
goes contrary to what one would expect.

Thanks


const int TOTAL_RUNS=1000000;
const int MAX_ITEMS=1000;

start=Environment.TickCount;

for (int y=0; y<TOTAL_RUNS; y++)

for (int x=0; x<MAX_ITEMS; x++)

count+=items[x];

end=Environment.TickCount;

Console.WriteLine("Safe Code Constant time:{0}",end-start);



//Test1 - safe mode using length property

count=0;

start=Environment.TickCount;

for (int y=0; y<TOTAL_RUNS; y++)

for (int x=0; x<items.Length; x++)

count+=items[x];

end=Environment.TickCount;

Console.WriteLine("Safe Code time:{0}",end-start);
WXS
2/14/2005 8:33:01 PM
I should point out when I tried to use properties of my own class I could not
reproduce this behavior, are arrays .Length field and other fields heavily
optimized even over constants???


This is using C# Visual Studio 2003.


[quoted text, click to view]
Paul Hill
2/15/2005 1:39:05 AM
[quoted text, click to view]

No. What happens is for() loops that use Array.Length are special-cased to
avoid bounds-checking on each access in the loop body, as the runtime can
guarantee it's not out of bounds, which is something it can't do for the
constant value.

Hope this helps!
WXS
2/15/2005 5:11:03 AM
It's interesting though if I use an unsafe code block with pointer or without
pointer access it is generally slower than the normal safe code blocks.

Are there any other optimizations like that .NET is doing, are they
documented anywhere? That could be very useful information to know.

Thanks,
Dave

[quoted text, click to view]
Paul Hill
2/15/2005 6:27:05 AM
It's interesting though if I use an unsafe code block with pointer or without
pointer access it is generally slower than the normal safe code blocks.

Kind of ironic, yeah? It's possible the JIT gets scared to optimise stuff
it can't verify.

[quoted text, click to view]

Yep:

http://msdn.microsoft.com/library/en-us/dndotnet/html/dotnetperftechs.asp
http://msdn.microsoft.com/library/en-us/dndotnet/html/dotnetperftips.asp

That's the big list, although there's other little gotcha's that are more
language specific. The Big Idea is that .NET is optimised for the general
case; almost always trying to optimise makes it slower.
WXS
2/15/2005 7:27:02 AM
Thanks very helpful!
Dave


[quoted text, click to view]
AddThis Social Bookmark Button