Re: Execution time of code?
On Mar 6, 8:50 am, c...@mailvault.com wrote:
On Mar 5, 11:55 pm, "Alf P. Steinbach" <al...@start.no> wrote:
I've done some performance testing on Windows and Linux
--www.webEbenezer.net/comparison.html. On Windows I use clock
and on Linux I use gettimeofday. From what I can tell
gettimeofday gives more accurate results than clock on Linux.
Depending on how this thread works out, I may start using the
function Victor mentioned on Windows.
On Unix based machines, clock() and gettimeofday() measure
different things. I use clock() when I want what clock()
measures, and gettimeofday() when I want what gettimeofday()
measures. For comparing algorithms to see which is more
effective, this means clock().
Victor is right about one thing: the implementation of clock()
in VC++ is broken, in the sense that it doesn't conform to the
specification of the C standard, e.g. that "The clock function
returns the implementation's best approximation to the processor
time used by the program since the beginning of an
implementation-defined era related only to the program
invocation." The last time I checked, the clock() function in
VC++ returned elapsed time, and not processor time. (Of course,
if you run enough trials, on a quiescent machine, the functions
involved are pure CPU, and the goal is just to compare, not to
obtain absolute values, the information obtained is probably
Of course, neither the C standard nor Posix are very precise
about what is meant by "processor time". Depending on what you
are trying to do, the function times() or some of the timer_...
functions might be more appropriate, at least under Unix (but I
presume that Windows also has something similar). But I
wouldn't bother until I'd determined that clock() wasn't
sufficient. (The Unix command time, for example, will probably
use gettimeofday for the real time, and times for the user and
For the rest: if you have a large application which is running
slow, you need a profiler, to determine where it is running
slow. Having found the critical function, however, if often
makes sense to write up a quick benchmark harness to compare
different possible implementations of the function, in order to
determine which one is best without having to rebuild and
remeasure the entire application each time.
James Kanze (GABI Software) email:email@example.com
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