[Tutor] Timing a python program

Albert-Jan Roskam fomcl at yahoo.com
Sat Jan 17 12:45:44 CET 2015

----- Original Message -----
> From: CL Talk <cltalk at gmail.com>
> To: Danny Yoo <dyoo at hashcollision.org>
> Cc: Python Tutor Mailing List <tutor at python.org>
> Sent: Saturday, January 17, 2015 4:04 AM
> Subject: Re: [Tutor] Timing a python program
>T hanks to all the responses. I have started looking at the
> documentation and will be implementing it. Thanks for the direction.
> I only have megabytes of data files. I think that the program can be
> optimized and am hoping that with the profiler I will be able to get
> some insight.

Btw, if you use Ipython, things get even easier:
%run -t -N3 /path/to/script.py  # run the program 3 times, show how much time it took

%run -p /path/to/script.py  # run cProfile
%timeit <python snippet here>

for example: 

In [1]: %timeit [i ** 2 for i in range(10)]
1000000 loops, best of 3: 1.02 µs per loop

In [2]: %timeit (i ** 2 for i in range(10))
1000000 loops, best of 3: 535 ns per loop

while we are at it, using the PDB debugger is also very easy:

%run -d -b10 /path/to/script.py  # run pdb, with a breakpoint on line10

%run -d -b myotherfile.py:20 script.py  # run program script.py that uses myotherfile.py, and put a breakpoint in line 20 of 

More information about the Tutor mailing list