How do I profile a Python script?

Chris Lawlor

Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With Python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to __main__.

What is a good way to profile how long a Python program takes to run?

Chris Lawlor

Python includes a profiler called cProfile. It not only gives the total running time, but also times each function separately, and tells you how many times each function was called, making it easy to determine where you should make optimizations.

You can call it from within your code, or from the interpreter, like this:

import cProfile
cProfile.run('foo()')

Even more usefully, you can invoke the cProfile when running a script:

python -m cProfile myscript.py

To make it even easier, I made a little batch file called 'profile.bat':

python -m cProfile %1

So all I have to do is run:

profile euler048.py

And I get this:

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}

EDIT: Updated link to a good video resource from PyCon 2013 titled Python Profiling
Also via YouTube.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to profile CPU usage of a Python script?

In Python script, how do I set PYTHONPATH?

How do I make a python script executable?

How do I make a python script executable?

How can I profile a multithread program in Python?

How can you profile a Python script?

How do I profile memory usage in Python?

How do I abort the execution of a Python script?

How do I profile the EDT in Swing?

How do I parse Python Script(py) file into Java POJOs?

How do I connect oracle DB with my python script?

How do I change provision profile on Xcode?

How Do I Create, Manage & Validate Sessions For Login & Profile Pages in Python With MongoDB as Database?

How do I get the profile URL of a user?

How do I let the user pause a python script at predefined points?

How do I profile F# script

How do i hide button in user profile

How do I go from a bash script that calls a python script to a python script calling a bash script?

How do I create a desktop entry to launch a Python script?

How do I set the default terminal profile?

How do I execute a bash function defined in .profile using python subprocess module?

How do I slice strings in Google Apps Script like in Python?

How do I call a sed command in a python script?

How can I profile a shell script?

How do I explain the contents of the profile file

How do I compute an averaged profile in OpenCV

How do I view the Python Codes inside a Python Script?

How do I install and run a python script

How do I create a new custom profile in Windows Terminal from a Powershell script?