mirror of
https://github.com/jlengrand/jlengrand.github.io.git
synced 2026-03-10 00:21:19 +00:00
2.4 KiB
2.4 KiB
layout, status, published, title, author, author_login, author_email, author_url, wordpress_id, wordpress_url, date, categories, tags, comments
| layout | status | published | title | author | author_login | author_email | author_url | wordpress_id | wordpress_url | date | categories | tags | comments | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| post | publish | true | Profiling a Python Script | Julien Lengrand-Lambert | jlengrand | julien@lengrand.fr | http://www.lengrand.fr | 546 | http://www.lengrand.fr/?p=546 | 2012-02-29 11:43:08.000000000 +01:00 |
|
|
Here are some simple ways to profile Python scripts.I heavily use this to check my Project Euler solutions.
SOLUTION 1:
The main common option would be to use the profile (or cprofile) module.There are two different ways of using it :- As a module, by directly running
{% highlight python %}
python -m cProfile script.py
{% endhighlight %}
- In your code, by importing the utilities
{% highlight python %}
import cProfile
cProfile.run('function()') # in your __main__
{% endhighlight %}
Bonus : You can use several options for sorting results using the -s switch (cumulative/name/time/file sorting are available).
SOLUTION 2:
If you want to avoid using a command line, or you don't have the profile module installed; here os another possibility. There is also the timeit module available.{% highlight python %} import timeit t1 = timeit.Timer("function()", "from main import function") print t1.timeit(1) {% endhighlight %}
I use this option on Eclipse because I didn't want to install the profile module on Windows.
This is however less clear, and way less detailed while still useful :).
Choose you profiler option and get on Project Euler !