Python/Profilers: Difference between revisions

From Omnia
Jump to navigation Jump to search
m (Kenneth moved page Python/Profiling to Python/Profilers)
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Profiling ==
== Python Profiling with Profilers ==




Line 6: Line 6:
The files cProfile and profile can also be invoked as a script to profile another script. For example:
The files cProfile and profile can also be invoked as a script to profile another script. For example:
  python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)
  python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)
Sort order
-s
See https://docs.python.org/3/library/profile.html#pstats.Stats.sort_stats
'cumulative' SortKey.CUMULATIVE cumulative time
'cumtime' N/A cumulative time
-r reverse  # put at bottom
---
tail -f /var/log/syslog | python3 -m profile -s cumtime /usr/local/bin/lolcat
tail -f /var/log/syslog | python3 -m cProfile -s cumtime /usr/local/bin/lolcat
import cProfile
import re
cProfile.run('re.compile("foo|bar")')
Save to file:
import cProfile
import re
cProfile.run('re.compile("foo|bar")', 'restats.txt')

Latest revision as of 07:55, 4 August 2024

Python Profiling with Profilers

https://docs.python.org/3/library/profile.html

The files cProfile and profile can also be invoked as a script to profile another script. For example:

python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)

Sort order

-s
See https://docs.python.org/3/library/profile.html#pstats.Stats.sort_stats
'cumulative' SortKey.CUMULATIVE cumulative time
'cumtime' N/A cumulative time
-r reverse  # put at bottom

---

tail -f /var/log/syslog | python3 -m profile -s cumtime /usr/local/bin/lolcat
tail -f /var/log/syslog | python3 -m cProfile -s cumtime /usr/local/bin/lolcat
import cProfile
import re
cProfile.run('re.compile("foo|bar")')

Save to file:

import cProfile
import re
cProfile.run('re.compile("foo|bar")', 'restats.txt')