3

Possible Duplicate:
Python: Time a code segment for testing performance (with timeit)

In C++, you can time blocks of code fairly easily, see code below. Is there any way to do this python? (easily) Thanks!

time_t startTime = clock();

// Do stuff

time_t endTime = clock();

cout << "Difference in time(milliseconds) : " << endTime - startTime << endl;
Community
  • 1
  • 1
64bitman
  • 102
  • 1
  • 2
  • 15
  • also: http://stackoverflow.com/questions/766335/python-speed-testing-time-difference-milliseconds http://stackoverflow.com/questions/1557571/how-to-get-time-of-a-python-program-execution – David Hall Feb 23 '12 at 23:33

4 Answers4

14

Word-for-word translation of your code

import datetime
start = datetime.datetime.now()
// do stuff
finish = datetime.datetime.now()
print(finish-start)
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
13

Try using the profilers available in the standard library.

Below is an example how to profile a script using cProfile from a command line. cProfile is one of the profilers available in all Python distributions.

$ python -m cProfile euler048.py

1007 function calls in 0.061 CPU seconds

Ordered by: standard name
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.061    0.061 <string>:1(<module>)
 1000    0.051    0.000    0.051    0.000 euler048.py:2(<lambda>)
    1    0.005    0.005    0.061    0.061 euler048.py:2(<module>)
    1    0.000    0.000    0.061    0.061 {execfile}
    1    0.002    0.002    0.053    0.053 {map}
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler objects}
    1    0.000    0.000    0.000    0.000 {range}
    1    0.003    0.003    0.003    0.003 {sum}
jacwah
  • 2,727
  • 2
  • 21
  • 42
gfortune
  • 2,589
  • 14
  • 14
4

You may wish to checkout the timeit module, it is extremely handy for timing small snippets of code.

Typical example:

from timeit import Timer

def foo():
    # some code here

t1 = Timer("""foo()""", """from __main__ import foo""")
print t1.timeit(1000)  # runs foo() 1000 times and returns the time taken
Adam Parkin
  • 17,891
  • 17
  • 66
  • 87
1

I'd break the section you want to time into a function and put a timing decorator around it that times the code.

This saves on code duplication and has he added benefit that you can store/log the function name and args with the timing stats.

Matt Alcock
  • 12,399
  • 14
  • 45
  • 61
  • If you want this kind of control and statistics I'd strongly advise _against_ doing that. It's like inventing the wheel again. Use a profiler instead! This is exactly what they do, _without littering your source code_. [See this answer](http://stackoverflow.com/a/9423176/2073469) – jacwah Jan 02 '14 at 13:38
  • 1
    I agree with fridge to some extent. Sometimes you want your code to log and monitor performance as it lives/runs in the real world a profiler isn't a good solution here. – Matt Alcock Jan 03 '14 at 17:16
  • That's true, good point. – jacwah Jan 03 '14 at 17:41