Created
February 17, 2020 14:43
-
-
Save Olshansk/9e693eaaca3153a8ff9a2584629388f9 to your computer and use it in GitHub Desktop.
An example of how to profile a simple python script using print_stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile, pstats | |
import time | |
from random import randint | |
def sleep1(): | |
time.sleep(0.1) | |
def sleep2(): | |
time.sleep(0.2) | |
def sleep3(): | |
time.sleep(0.3) | |
pr = cProfile.Profile() | |
pr.enable() | |
for _ in range(10): | |
for _ in range(randint(1, 10)): | |
sleep1() | |
for _ in range(randint(1, 10)): | |
sleep2() | |
for _ in range(randint(1, 10)): | |
sleep3() | |
pr.create_stats() | |
ps = pstats.Stats(pr) | |
ps.print_stats() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment