python source benchmark
Cedric Adjih
adjih at crepuscule.com
Tue Oct 3 06:47:27 EDT 2000
seung-won hwang <shwang5 at students.uiuc.edu> wrote:
> Hello,
>
> I'm trying to benchmark python source with clock() function, but
> my code below returns negative value. I am not sure if my code is
> wrong or clock() is not suitable for getting CPU time... Any comment
> would help me a lot. Thanks.
>
> My code:
> scl=time.clock()
> ...
> fcl=time.clock()
> print "Execution Time: =", fcl-scl
> Reference:
> clock ()
> Return the current CPU time as a floating point number expressed in
> seconds. The precision, and in fact the very definition of the meaning of
> ``CPU time'', depends on that of the C function of the same name, but in
> any case, this is the function to use for benchmarking Python or timing
> algorithms
Well according to Linux documentation for clock(3):
BUGS
The C standard allows for arbitrary values at the start of
the program; take the difference between the value
returned from a call to clock() at the start of the pro
gram and the end to get maximum portability.
Note that the time can wrap around. On a 32bit system
where CLOCKS_PER_SEC equals 1000000 this function will
return the same value approximately every 72 minutes.
Since CLOCKS_PER_SEC is actually defined to be 1000000 in
/usr/include/bits/time.h, on my Linux machine, you will have
a wrap-around after 72 minutes (or less) under the same circumstances.
On Unix, there is a os.times() that probably suffers from the
same problem, and a module resource (resource.getrusage(...)), which
isn't compiled in on my machine.
I usually use the /bin/time command, time.time() (on a lightly
loaded machine), or the profile module of Python for computing
the time spent in a program (as another person indicated here).
-- Cedric
More information about the Python-list
mailing list