Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save normanlmfung/75cacda0065a717cf0fd311ae372ee45 to your computer and use it in GitHub Desktop.
Save normanlmfung/75cacda0065a717cf0fd311ae372ee45 to your computer and use it in GitHub Desktop.
python_profile_example_nested_func
import cProfile
from pstats import Stats, SortKey
import random
import hashlib
import time
def _perf_test_A():
for i in range(1000000):
i = random.randint(0, 100)
# _perf_test_B(i)
def _perf_test_B(i : int):
s = str(i)
print(s)
_perf_test_C(s.encode('utf-8'))
def _perf_test_C(s):
hash = hashlib.md5(s)
if __name__ == "__main__":
# https://docs.python.org/3/library/profile.html
with cProfile.Profile() as pr:
start = time.time()
res = _perf_test_A()
print(f"Ellapsed time: {time.time() - start} s")
with open('profiling_stats.txt', 'w') as stream:
stats = Stats(pr, stream=stream)
stats.strip_dirs()
stats.sort_stats(SortKey.CUMULATIVE)
stats.dump_stats('.prof_stats')
stats.print_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment