I noticed that C# jitter produces measurably slower code than C++ compiler, even if there are no "managed overhead" constructs used (such as arrays with checked indexing).
To quantify it, I timed the following simple loop:
public static int count = 1000000000;
public static int Main()
{
int j = 0;
for (int i = 0; i < count; ++i)
{
j += (i % 2 == 0) ? ((i + 7) >> 3) : (i * 7);
}
return j;
}
This loop takes 3.88s to execute (compiled with /o). Equivalent loop compiled with VC 2010 (-O2) takes 2.95s.
To verify that inferior code was actually generated I compared machine codes: created a listing (/FAs) from VC compiler, and attached a debugger to C# program (after the loop was completed).
Indeed, C++ version is using some clever tricks. For example to avoid costly multiplication by 7, there is a separate register that is incremented by 7 every loop count. C# version does the multiplication (imul) every time. There are other differences as well.
I understand that C# jitter has a lot less time to compile the code at runtime than VC at build time. But e.g. Java jitter is dynamically optimizing frequently used methods. C# doesn't seem to be doing it.
My question is: are there plans to improve C# jitter in the future framework versions?