Re: [GD-Windows] VC.NET 'Optimization'
Brought to you by:
vexxed72
From: yogiwp <yo...@gm...> - 2005-04-12 18:54:15
|
----- Original Message ----- From: Kent Quirk > fib is declared locally, and no one ever takes its address. The compiler is pretty safe in assuming > that no sane code could possibly affect the value of fib. Similarly, the fibonacci function only > takes a constant argument. If it's in the same source file, it might even know that the function has > no aliasing issues. Consequently, it's reasonable to optimize away the creation of the fib variable > and simply embed the function call in the printf call. > > My guess is that what the compiler did was optimize this to: > > ---------------------------------------------------- > timer.Restart(); > printf( "time=%f, result=%d\n", timer.GetElapsed(), fibonacci(42) ); > ---------------------------------------------------- Yes, this is exactly what happened (disassembly below). Sigh. I must say that this makes my worry. Btw, #pragma optimize("a",off) and/or ("w",off) have no effect; it's still generating the exact same code. Declaring msTime as 'volatile' does fix it. ------------------------------------------------------------------------------- ; 5215 : timer.Restart(); lea ecx, DWORD PTR _timer$[esp+148] call ?Restart@CRealTimer@Torture@@QAEXXZ ; Torture::CRealTimer::Restart ; 5216 : int fib = fibonacci( 42 ); ; 5217 : float msTime = timer.GetElapsed(); lea ecx, DWORD PTR _timer$[esp+148] call ?GetElapsed@CRealTimer@Torture@@QAEMXZ ; Torture::CRealTimer::GetElapsed push 41 ; 00000029H call ?fibonacci@@YAHH@Z ; fibonacci push 40 ; 00000028H mov edx, eax call ?fibonacci@@YAHH@Z ; fibonacci add esp, 8 add edx, eax ; 5218 : printf( "msTime=%f, result=%d\n", msTime, fib ); push edx sub esp, 8 fstp QWORD PTR [esp] push OFFSET FLAT:??_C@_0BG@BEAEGCGA@msTime?$DN?$CFf?0?5result?$DN?$CFd?6?$AA@ call _printf add esp, 16 ; 00000010H ------------------------------------------------------------------------------- (There are 2 fibonacci() calls because of inlining -- fibonacci() is a recursive function calling itself twice.) Yogi Wahyu Prasidha |