Re: Does 'if' have performance overhead
On Oct 19, 1:51 pm, Stephan Rose <nospam.no...@screwspammers.com>
On Fri, 19 Oct 2007 07:33:09 +0100, Michael Bell wrote:
In message <ff1mbf$hl...@murdoch.acc.Virginia.EDU>
Kai-Uwe Bux <jkherci...@gmx.net> wrote:
Somewhere in a tutorial i read that if statement has performance
overheads as code within the if statement cannot take benefit of
pipeling of microprocessor and also that the compiler cannot
agressively optimize that code.
It takes more time? I'm not sure how you would measure that! Surely not
by sitting in front of the screen with a stop-watch?
The only workable thing I can think of is to put a line before the block
under test to read the internal clock, a second line at the end of the
block, and the difference is the time taken. How exactly would you do
such a thing?
That is a little platform dependent really.
The standard provides a function, clock(), expressedly for this.
Regretfully, the implementation in Windows is poor enough to be
In my case, I have a class called CpuTimer that works both under Windows
and Linux that can do high precision timing.
The precision comes from repeating the operation millions of
times. I generally don't consider my measurements significant
unless I've repeated enough for the actual execution time to be
around five minutes.
Then when I want to profile something, I will usually isolate the one
single function I want to profile, write a small test case along with
test data. The amount of data I generate depends on the complexity of the
function, that can range anywhere from a few hundred data items to a few
You also have to worry about ensuring that the optimizer doesn't
realize that your function has no real impact on the final
output, and suppresses it entirely.
Then I measure the time it took to process the complete data set, which
then divided by the number of data items in the set gives me the average
execution time per function call.
It's not 100% precise of course as too many uncontrollable factors can
affect execution speed, such as OS background tasks and such. However, it
is good enough to tell if a change I have made has made things better or
worse which is ultimately the only thing I am really concerned about.
Another thing you probably want to do is execute the function
once before starting the timing, to ensure that it is paged in,
and in cache, if it fits.
James Kanze (GABI Software) email:firstname.lastname@example.org
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34