Python Line-by-line Profiler (line_profiler and kernprof)
The following is a quick and dirty guide to getting started with line_profiler, a Python line-by-line profiler, on Fedora.
- Build and install the python-line_profiler package
Create a file called
test.pywith the code belowimport random, time def sleep(): seconds = random.randint(0, 5) print 'Sleeping %s seconds' % seconds time.sleep(seconds) @profile def test(): sleep() sleep() sleep() test()Profile
test.py[silas@silas ~]$ kernprof.py -l test.py Sleeping 4 seconds Sleeping 5 seconds Sleeping 2 seconds Wrote profile results to test.py.lprofView the results
[silas@silas ~]$ python -m line_profiler test.py.lprof Timer unit: 1e-06 s File: test.py Function: test at line 8 Total time: 10.9994 s Line # Hits Time Per Hit % Time Line Contents ============================================================== 8 @profile 9 def test(): 10 1 3999416 3999416.0 36.4 sleep() 11 1 4999982 4999982.0 45.5 sleep() 12 1 1999990 1999990.0 18.2 sleep()
NOTE: I have a package review up for line_profiler and it should be available via yum eventually.