RE: VC.NET 'Optimization'

"Lewin, Gareth" <[email protected]> Tue, 12 Apr 2005 12:02:21 -0700
Newsgroups gmane.games.devel.windows
Message-ID <[email protected]>
I fail to see why this worries you, the optimiser is making a (valid)
assumption about your code, and generating the fastest sequence it can.

As a side note, just flip the order of your params to printf, that
should give you want you want. 

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
yogiwp
Sent: Tuesday, April 12, 2005 11:52 AM
To: [email protected]
Subject: Re: [GD-Windows] VC.NET 'Optimization'

----- 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



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide Read honest & candid
reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_ide95&alloc_id396&op=click
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_idU5