Skip to content

Instantly share code, notes, and snippets.

@Olshansk
Created February 17, 2020 14:43
Show Gist options
  • Save Olshansk/9e693eaaca3153a8ff9a2584629388f9 to your computer and use it in GitHub Desktop.
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
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