[Tutor] Timing a loop

amk at amk.ca amk at amk.ca
Wed Oct 22 07:27:55 EDT 2003


On Tue, Oct 21, 2003 at 11:08:05PM -0400, Ben Mazer wrote:
> I also looked at time.clock(), but I can't get that to display an 
> accurate readout, so I dont think that works (or Im not using it right).

time.clock() displays CPU time for the process; for an I/O bound task like
file creation, the process likely won't use much CPU -- it'll spend its time
idly waiting for disk blocks to be read.

You want time.time(), which returns "wall clock" time.  Your results will
therefore vary depending on your system's load; if cron runs in the middle
of your timing interval, you'll measure a longer time, so usually people
make several runs, discard outliers, and average them.

> Any help you could provide on timing a loop would be really helpful.

If you're using a big loop such as 'for i in range(100000)', you don't want
to measure the time to create the list of 100000 integer objects, so you
should write something like:

L = range(100000)
s = time.time()
for i in L:
    ....
e = time.time()
print 'Total time:', e-s, 'sec'.

> OT: How many files can reiserfs hold? I keep hitting "file system full" 
> limits when I get into large amounts of files (hundreds of thousands).

Look at the output of os.statvfs('/filesystem').   files, ffree, favail are
the number of free i-nodes.  (Though on my ReiserFS partition they're all
reported as 2**32-1 -- does ReiserFS have a fixed i-node limit?)

--amk



More information about the Tutor mailing list