Performance is important for games, luckily there are many Python profiling tools. Profiling is about building a profile of a software program in order to collect information about memory usage or time complexity.
cProfile
is a C extension introduced in Python 2.5. It can be used for deterministic profiling. Deterministic profiling means that the time measurements are precise and no sampling is used. Contrast this with statistical profiling, where measurements come from random samples.
The following steps will help you profile your code:
python -m cProfile -o collision_demo.profile collision_demo.py
python -m pstats collision_demo.profile Welcome to the profile statistics browser.
collision_demo.profile% help Documented commands (type help <topic>): ======================================== EOF add callees callers help quit read reverse sort stats strip
sort
command:collision_demo.profile% sort Valid sort keys (unique prefixes are accepted): stdname -- standard name nfl -- name/file/line pcalls -- call count file -- file name calls -- call count time -- internal time line -- line number cumulative -- cumulative time module -- file name name -- function name
stats
:collision_demo.profile% sort calls collision_demo.profile% stats 3 380943 function calls (380200 primitive calls) in 18.056 seconds Ordered by: call count List reduced from 801 to 3 due to restriction <3> ncalls tottime percall cumtime percall filename:lineno(function) 52156 0.013 0.000 0.013 0.000 {method 'endswith' of 'str' objects} 31505/31368 0.003 0.000 0.003 0.000 {len} 27573 0.022 0.000 0.022 0.000 {method 'lower' of 'str' objects}
We profiled the collision demo. The following table summarizes the profiler output:
Column |
Description |
---|---|
|
Number of calls |
|
Total time spent in a function |
|
Time per call, calculated by dividing the total time by the calls count |
|
Cumulative time spent in function and functions called by the function, including recursive calls |