time (was Re: lists, performance..)

Pearu Peterson pearu at cens.ioc.ee
Thu Oct 31 12:48:20 EST 2002


On 31 Oct 2002, Alex Martelli wrote:

> <posted & mailed>
> 
> gabor wrote:
>         ...
> > btw. is there a timing function in python?
> > like 'time' in unix..
> 
> Module time in the Python standard library supplies a slightly
> different functionality from Unix's time command.  In particular
> time.clock() returns "what time it is right now" -- CPU time on
> Unix-ish systems, very accurate elapsed-time on Windows.  So, to
> time how long some operation takes, the standard idiom is:
> 
> import time
> 
> start = time.clock()
> # the operations you want to time
> stend = time.clock()
> 
> print "It took %.2f seconds" % (stend-start)

While measuring time for operations, it is possible that some other
independent system/user process triggers swapping, for example, so that 
the measuring process gets less CPU, and therefore using
"time.clock()" may give unrealistic timing results. To get more reliable
timings, one has to run the measuring programs several times. And that may
be time consuming.

Another approach, at least under linux, would be to use "user time" that
gives how much the measuring process acctually used CPU. This avoids the
need to run the measuring programs several times as the result does not
depend (too much) on how OS gives CPU time to different processes.

Here follows a simple function for linux OSes that returns "user time" in
jiffies:

import os
def jiffies(_proc_pid_stat = '/proc/%s/stat'%(os.getpid())):
  """ Return number of jiffies (1/100ths of a second) that this
      process has been scheduled in user mode. See man 5 proc. """
  f=open(_proc_pid_stat,'r')
  l = f.readline().split(' ')
  f.close()
  return int(l[13])

so that the linux compatible idiom for timing would be:

start = jiffies()
# the operations you want to time
stend = jiffies()

print "It took %.2f seconds" % (100*(stend-start))

Thoughts?

Pearu





More information about the Python-list mailing list