tags:

views:

414

answers:

7

I have a command line program in Python, that takes quite a while to finish. I want to know the exact time it takes to finish running.
I've looked at the timeit module, but it seems it's only for small snippets of code. I want to time the whole program.

+6  A: 

In Linux or UNIX:

time python yourprogram.py

In Windows, see this Stackoverflow discussion: http://stackoverflow.com/questions/673523/how-to-measure-execution-time-of-command-in-windows-command-line

steveha
+8  A: 

The simplest way in python:

start_time = time.time()
main()
print time.time() - start_time, "seconds"

This assumes that your program takes at least a tenth of second to run.

rogeriopvl
this calculates the real time though (including time used by other programs) so it will seem to take more time when your computer is busy doing other stuff
newacct
on Windows, do the same thing, but use time.clock() instead of time.time(). You will get slightly better accuracy.
Corey Goldberg
Paul's answer is better, see http://stackoverflow.com/questions/1557571/how-to-get-time-of-a-python-program-execution/1557906#1557906
Sorin Sbarnea
+2  A: 

The solution of rogeriopvl works fine, but if you want more specific info you can use the python built-in profiler. Check this page:

http://docs.python.org/library/profile.html

a profiler tells you a lot of useful information like the time spent in every function

wezzy
Ahh that's brilliant!. Will be using this from now on
Dominic Bou-Samra
+3  A: 

You can also get the execution time from command line, like this:

python -mtimeit your_script.py
Nick D
+1  A: 
start_time = time.clock()
main()
print time.clock() - start_time, "seconds"

time.clock() returns the processor time, which allows us to calculate only the time used by this process (on Unix anyway). The documentation says "in any case, this is the function to use for benchmarking Python or timing algorithms"

newacct
time.time() is best used on *nix. time.clock() is best used on Windows.
Corey Goldberg
+3  A: 

I put this timing.py module into my own site-packages directory, and just insert import timing at the top of my module:

import atexit
from time import clock

def secondsToStr(t):
    return "%d:%02d:%02d.%03d" % \
        reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
            [(t*1000,),1000,60,60])

line = "="*40
def log(s, elapsed=None):
    print line
    print secondsToStr(clock()), '-', s
    if elapsed:
        print "Elapsed time:", elapsed
    print line
    print

def endlog():
    end = clock()
    elapsed = end-start
    log("End Program", secondsToStr(elapsed))

def now():
    return secondsToStr(clock())

start = clock()
atexit.register(endlog)
log("Start Program")

I can also call timing.log from within my program if there are significant stages within the program I want to show. But just including import timing will print the start and end times, and overall elapsed time. (Forgive my obscure secondsToStr function, it just formats a floating point number of seconds to hh:mm:ss.sss form.)

Paul McGuire
This is a real clean solution that also works if you press Ctrl-C to stop the program.
Sorin Sbarnea
+1  A: 

Even better for Linux: /usr/bin/time

$ /usr/bin/time -v python rhtest2.py

    Command being timed: "python rhtest2.py"
    User time (seconds): 4.13
    System time (seconds): 0.07
    Percent of CPU this job got: 91%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:04.58
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 0
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 15
    Minor (reclaiming a frame) page faults: 5095
    Voluntary context switches: 27
    Involuntary context switches: 279
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

Normally, just time is a simpler shell builtin that shadows the more capable /usr/bin/time.

kaizer.se