[SciPy-User] Timing optimization of python code...

Jonathan Rocher jrocher at enthought.com
Fri Feb 18 11:27:34 EST 2011


HI Anthony,

several things I would recommend for you to use to do that:
- if you use Ipython, you can use the "magic" function %timeit. It is quick
and pretty smart as it runs a given command many times and make an average.
Just do in the ipython prompt for example:
In [12]: a= None
In [17]: %timeit a is None
10000000 loops, best of 3: 84.9 ns per loop

- if you want a profiler that will analyse a large code and tell you how
much time is spent in each function, I recommend the line_profiler package:

 http://pypi.python.org/pypi/line_profiler

This provides a script called kernprof.py which can be used to run a
script under the standard cProfile profiler or the eponymous
line-based profiler.

 $ kernprof.py code.py
Wrote profile results to code.py.prof

You can use the stdlib pstats module as a script to view the results:

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

$ python -m pstats
Welcome to the profile statistics browser.
% help

Documented commands (type help <topic>):
========================================
EOF  add  callees  callers  quit  read  reverse  sort  stats  strip

Undocumented commands:
======================
help

% read ./code.py.prof
./code.prof% strip
./code.py.prof% sort time
./code.py.prof% stats 10

The last command gives you the 10 functions the code spend the most time in.

Hope this helps,
Jonathan

On Fri, Feb 18, 2011 at 10:18 AM, Anthony Palomba <apalomba at austin.rr.com>wrote:

> I am trying to optimize the timing of my python source.
>
> Does scipy have a high resolution timer that would allow
> me to measure latency of functions in order to optimize things?
>
>
>
> Thanks,
> Anthony
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>


-- 
Jonathan Rocher,
Enthought, Inc.
jrocher at enthought.com
1-512-536-1057
http://www.enthought.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110218/e1f00cff/attachment.html>


More information about the SciPy-User mailing list