HighTechTalks DotNet Forums  

Managed vs Unmanaged Bare Bones Performance Test

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Managed vs Unmanaged Bare Bones Performance Test in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #11  
Old   
Willy Denoyette [MVP]
 
Posts: n/a

Default Re: Managed vs Unmanaged Bare Bones Performance Test - 04-20-2007 , 05:27 AM






"Ben Voigt" <rbv (AT) nospam (DOT) nospam> wrote

Quote:
This kind of benchmarh is meaningless..
The reason for the huge difference is that the C++ compiler hoists the loop, as it sees
no sensible reason to call an empty function 50000 times, the C# compiler does not do
this, it simply calls the function which only contains a ret.

Inlining and optimizing away a call to an empty function is well within the capabilities
of the CLR JIT.

True for optimized builds, the call is hoisted, but the loop is not hoisted by the JIT, the
C++ compiler (optimized build) effectively hoists the loop.
What's produced by the JIT depends on the version of the CLR.

this snip of the code:
for (int i = 0; i < 50000; i++)
{
Run(i);
}

is turned into into:

xor r11d,r11d
add r11d,4
cmp r11d,0C350h
jl 00000642`80150341 (jump to add r11d, 4 if less than)


by the JIT64, while the JIT32 (both v2 of the CLR), produces

xor eax,eax
add eax,1
cmp eax,0C350h
jl 001e014b (jump to add eax, 1 if less than)

see the subtle difference:
add r11d, 4
and
add eax, 1

here the JIT64 is cheating , no big deal in this case, but I would prefer some more
consistent behavior across JIT versions, here I mean hoist the loop, or keep the loop as is,
but don't cheat.

Willy.





Reply With Quote
  #12  
Old   
Willy Denoyette [MVP]
 
Posts: n/a

Default Re: Managed vs Unmanaged Bare Bones Performance Test - 04-20-2007 , 06:15 AM






"Ben Voigt" <rbv (AT) nospam (DOT) nospam> wrote

Quote:
"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote in message
news:MPG.2091f2b49096c93598daf1 (AT) msnews (DOT) microsoft.com...
Ben Voigt <rbv (AT) nospam (DOT) nospam> wrote:
This kind of benchmarh is meaningless..
The reason for the huge difference is that the C++ compiler hoists the
loop, as it sees no sensible reason to call an empty function 50000 times,
the C# compiler does not do this, it simply calls the function which only
contains a ret.

Inlining and optimizing away a call to an empty function is well within the
capabilities of the CLR JIT.

That was my thought too. I suspect it'll still perform the loop
iteration, however, whereas the C++ compiler may well have removed that
loop completely, which still means it's not a good benchmark.

Oh, and if it's desired not to have the loop optimized away, touch a volatile variable
from inside the function.
True, with the following results:

C# (JIT32)
244
0,0000681650880209635582175947
C# (JIT64)
86
0,0000240253998762412541258735

C++ (O2 switch) 32 bit and 64 bit
164
0.000045815878834

see, C++ is faster than JIT 32 but slower than JIT64 code, for this particular case (50000
iterations), however such kind of micro-benchmarks have absolutely no value. For instance
make the loop count an odd number (eg. 49999) and you get the same results for C# JIT64
and C++.
Note that the JIT32 is known as not to being a great loop optimizer ;-).

Willy.



Reply With Quote
Reply




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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.