RE: [GD-Windows] VC.NET 'Optimization'
Brought to you by:
vexxed72
From: Simon O'C. <si...@sc...> - 2005-04-11 22:05:01
|
Simply re-ordering the source without forcing optimisations off isn't guaranteed to do what you expect. Contrived example: imagine your = profiling code calls QueryPerformanceFrequency() and then code being profiled = later on makes a call to QueryPerformanceFrequency(); it's feasible that an = optimiser could detect two calls to that same Windows function and optimise them = into one call thus totally skewing your results. Personally (when not using an already proven profiler) I'd always = declare the timer object and even msTime as "volatile" to ensure the compiler doesn't consider them for optimization.=20 Alternatively you can use #pragma optimize to control exactly which code does and doesn=92t get optimised.=20 BTW: I'm not entirely sure whether that's a compiler bug or a valid optimisation - it depends what your Restart() function does. > -----Original Message----- > From: gam...@li...=20 > [mailto:gam...@li...] On=20 > Behalf Of yogiwp > Sent: 11 April 2005 14:47 > To: gam...@li... > Subject: [GD-Windows] VC.NET 'Optimization' >=20 > Hi all, >=20 > I've just waste alot of time debugging, and surprised that=20 > the root of problem come from VC.NET 2003 reordering stuff=20 > too aggresively. >=20 > This piece of code is supposed to measure time spent in fibonacci(): > ---------------------------------------------------- > timer.Restart(); > int fib =3D fibonacci( 42 ); > float msTime =3D timer.GetElapsed(); > printf( "time=3D%f, result=3D%d\n", msTime, fib ); > ---------------------------------------------------- > But the optimizer decided to move the fibonacci() call just before > printf() and after GetElapsed(), causing the timer to measure nothing. >=20 > Of course the bug does not manifest in debug builds, which=20 > made my head hurt even more :( >=20 > The fix is simply changing the last line to: > printf( "time=3D%f, result=3D%d\n", timer.GetElapsed(), fib ); >=20 > Anyone got bitten by this before? This is a bug, right? >=20 >=20 > Yogi Wahyu Prasidha >=20 >=20 >=20 >=20 --=20 No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.9.6 - Release Date: 11/04/2005 =20 |