First of all, forgive me, as my question may seem foolish, but I'm really curious why I get a performance boost in this very simple code.
here's the assembly code:
__asm {
mov eax, 0
mov ecx, 0
jmp startloop
notequal:
inc eax
mov ecx, eax
sub ecx, 2
startloop:
cmp eax, 2000000000
jne notequal
};
and this is C code:
long x = 0;
long ii = 0;
for(; ii < 2000000000; ++ii)
{
x = ii - 2;
};
C code takes approximately 1060 ms (in release build) to complete on my i5 2500k machine and the assembly finishes in 780ms. It's a ~25% gain in speed. I don't understand why do I get this result, because 25% is a big difference. Isn't the compiler smart enough to generate an equal assembly code that I've written?
BTW I'm using MSVC 2010.
Thanks
Here's the (asm) code that's being generated by MSVC
$LL3@main:
; Line 36
lea esi, DWORD PTR [eax-2]
inc eax
cmp eax, 2000000000 ; 77359400H
jl SHORT $LL3@main
what does lea instruction do in this case?
UPDATE 2
Thank you very much everyone. I just tested this code at work, on Nehalem xeon cpu and the results are identical here. Looks like for an unknown reason, that asm code runs faster on Sandy bridge.
ii = 2000000000, x = 1999999997
. If you need help with optimizations, code with "taboo" optimization isn't realistic.