[Tutor] Timing calculation (fwd)

Magnus Lycka magnus@thinkware.se
Sat, 21 Sep 2002 11:44:14 +0200


>Could someone please show me how to calculate the time, which is running by
>a test. My program is doing:
>...
>os.system( "make deja %s 2>&1 | tee %s" % (makeopts,spoolfile) )
>data =3D open( spoolfile ).read()
>...
>
>I need to get the time before and after running "make deja", then calculate
>the time between.

Well, it seems like you are running a unix of some sort.

Then you can always time things on the os level with "time make ..."

By using os.popen() instead of os.system(), the output of your os
command will end up in a file handle that you can read.

Look at the os.popen family of commands in the library manual. With
os.popen4, you will get two file handles back, one with stdin, and
another with both stdout and stderr.

So if you don't need the spoolfile for anything else:

import os

stin, stouterr =3D os.popen4('time make deja %s' % makeopts)
stin.close() # Don't pipe anything into the command
dump =3D stouterr.readlines() # but get the output and errors
stouterr.close()

# Two last lines should be timing info
data =3D "".join(dump[:-2])
timing =3D "".join(dump[-2:])

Naturally, you can also use python for the timing, but it
will give you less information, and include some time
actually spent in python.

The time module contains a time.time() function that returns
the number of seconds since midnight Jan 1 1970, and time.clock()
that returns the number of seconds since...well it depends on
the OS. You can use it as a timer anyway.

time.clock() will sometimes reach a maximum value and start
over from zero if your machine or program is running for long.
time.time() should never do that (before Jan 19 2038).

Python gets these times from different sources depending on
OS, and they have different resolution. With a quick measurement
I get these results on two AMD machines:

OS           |time.time()|time.clock()
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Windows 2000 |     10 ms | ~0.010 ms
Linux 2.4.3  | ~0.020 ms |     10 ms

Strange, huh? The Windows machine is a 700MHz Duron, and the
Linux box is a 233MHz K-6, which might explain the difference
in Linux time.time() and Windows time.clock(). I don't know
if the short times are limited by the OS or by Python delays.

But as you notice, apart from avoiding rollover, you get a
much better resolution with time.time() in Linux. In Windows
you get a much better resolution with time.clock(), and should
choose that when milli second resolution is needed, and
time.time() otherwise to avoid rollover problems.

So, finally, your code with the python timer:

import time
timer =3D time.time # or time.clock

cmd =3D "make deja %s 2>&1 | tee %s" % (makeopts,spoolfile)
start =3D timer()
os.system( cmd )
stop =3D timer()
print cmd, "took", stop-start, "seconds"
data =3D open( spoolfile ).read()

To check the resolution on the different platforms I did:

 >>> def x(f):
...     r =3D f()
...     while 1:
...             r1 =3D f()
...             if r1 !=3D r:
...                     print r1 - r
...                     break
...
 >>> import time
 >>> x(time.time)
0.00999999046326
 >>> x(time.clock)
1.03365092343e-005

(This is obviously Windows.)


--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se