The profile
module is the standard Python profiler.
Like the disassembler and the debugger, you can run the profiler from the command line:
$ profile.py hello.py hello again, and welcome to the show 3 function calls in 0.785 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.001 0.001 0.002 0.002 <string>:1(?) 1 0.001 0.001 0.001 0.001 hello.py:1(?) 1 0.783 0.783 0.785 0.785 profile:0(execfile('hello.py')) 0 0.000 0.000 profile:0(profiler)
It can also be used to profile part of a program, as Example 11-4 shows.
Example 11-4. Using the profile Module
File: profile-example-1.py import profile def func1(): for i in range(1000): pass def func2(): for i in range(1000): func1() profile.run("func2()")1003 function calls in 2.380 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 2.040 2.040 <string>:1(?)
1000 1.950 0.002 1.950 0.002 profile-example-1.py:3(func1)
1 0.090 0.090 2.040 2.040 profile-example-1.py:7(func2)
1 0.340 0.340 2.380 2.380 profile:0(func2())
0 0.000 0.000 profile:0(profiler)
You can modify the report to suit your needs, via the pstats
module.
3.143.22.23