[Tutor] time taken to execute certain task

Jack Trades jacktradespublic at gmail.com
Thu Mar 17 05:50:09 CET 2011

On Wed, Mar 16, 2011 at 9:40 PM, tee chwee liong <tcl76 at hotmail.com> wrote:

>  hi,
> i would like to know the time taken to execute a certain task in python. i
> used time.time and time.clock and i see the time taken is different? what is
> the right method to use?
> import time
> def testtime(num):
>     start=time.time()
>     #print start
>     for n in range(num):
>         #print n
>         print time.time()-start
> testtime(101)
> start = time.clock()
> for x in range(101):
>   y = x  # do something
> end = time.clock()
> print "Time clock elapsed = ", end - start, "seconds"
> thanks
> tcl

First you have to start by testing things that are similar.  I've rewritten
your tests to look exactly the same except for the time.time() or

In your first example, you are calculating an end time for each iteration of
the for loop.  In your second example you are only calculating the end time
once, after the for loop has finished.

Here's how I would compare them...

(I hope the indentation doesn't get hosed.  If it does I can repost.)

import time

def testtime(num):
  start = time.time()
  for n in range(num):
    1 + 1 # do something
  print time.time() - start

#===> 3.50475311279e-05

def testclock(num):
  start = time.clock()
  for n in range(num):
    1 + 1  # do something
  print time.clock() - start

#===> 0.0

Now that each test is exactly the same, you are in a better position to
judge the differences.  And there are some obvious differences!  Why?  To
determine that you need to find out just what each function is supposed to

The help() function will show you what each function (time or clock) is
supposed to return.  It looks like this...

    time() -> floating point number

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.

    clock() -> floating point number

    Return the CPU time or real time since the start of the process or since
    the first call to clock().  This has as much precision as the system

As you can see, time.time() returns the current time in seconds from the
Epoch, while time.clock() returns the time since the program started, or the
time since the first call to time.clock().

The "epoch" is an arbitrary point in the past (don't rely on it always being
the same).  So both time.time() and time.clock() return the number of
seconds, but each of them starts at a different time.

As to which one's best?  I don't know.  I know there are some profiling
tools that will give you a better idea about the performance of a function,
but I rarely use them so I'll let someone else give you advice on this.

For a quick-n-dirty test, either will work just fine.  I usually use
time.time(), but I only really ever do this "just for kicks".

Jack Trades
Pointless Programming Blog <http://pointlessprogramming.wordpress.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110316/1ea99a32/attachment-0001.html>

More information about the Tutor mailing list