Along my source code I try to capture and measure the time release of a segment in Python. How can I measure that segment pass time in a convenient way with good precision?
            Asked
            
        
        
            Active
            
        
            Viewed 7,195 times
        
    27
            
            
        - 
                    3I don't want to take credit for this method as my own, but look at this answer by Mike Dunlavey about random pausing that can be used rather than a profiler: http://scicomp.stackexchange.com/a/2719/1552 – Godric Seer Nov 08 '13 at 14:00
- 
                    And [*here is an example in python.*](http://stackoverflow.com/a/4299378/23771) – Mike Dunlavey Nov 08 '13 at 22:52
1 Answers
42
            Use a profiler.
Python's cProfile is included in the standard libary.
For an even more convenient way, use the package profilestats. Then you can use a decorator to just decorate the functions you want to profile:
from profilestats import profile
@profile
def my_function(args, etc):
    pass
This will cause a summary like this to be printed on STDOUT:
         6 function calls in 0.026 seconds
   Ordered by: cumulative time
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.026    0.026 some_code.py:3(some_func)
        2    0.019    0.010    0.026    0.013 some_code.py:9(expensive_func)
        2    0.007    0.003    0.007    0.003 {range}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
The much more useful info however is in the cachegrind.out.profilestats file generated. You can open this file with a tools that can visualize cachegrind statistics, for example RunSnakeRun and get nice, easy (or easier) to read visualizations of your call stack like this:
Update: Both pull requests for profilestats and pyprof2calltree have been merged, so they now support Python 3.x as well.
 
    
    
        Community
        
- 1
- 1
 
    
    
        Lukas Graf
        
- 30,317
- 8
- 77
- 92
- 
                    What version of Python does `profilestats` support? There is no indication on the page or even in any of the code that I see... – Izkata Nov 08 '13 at 15:35
- 
                    You're right, it doesn't indicate supported Python versions on it's PyPi page. It's just a tiny wrapper around `cProfile` ([`profilestats.py`](https://github.com/hannosch/profilestats/blob/master/profilestats.py) is basically all the code), but from a quick glance it's use of `file` instead of `open` means it wont work on Python 3.x as-is. It also depends on `pyprof2calltree`, which is 2.x only. However, both should be easily converted by the `2to3` script, I'm guessing that just no-one has gotten around to making that pull-request yet. – Lukas Graf Nov 08 '13 at 18:04
- 
                    1Python 3.x pull-requests done for [**`pyprof2calltree`**](https://github.com/pwaller/pyprof2calltree/pull/2) and [**`profilestats`**](https://github.com/hannosch/profilestats/pull/1). – Lukas Graf Nov 08 '13 at 20:41
 
     
    