Profiling - python

Profile Your Code

pr = cProfile.Profile()
pr.enable()
#code to execute
pr.print_stats(sort='tottime')
# other sorting methods here:
# https://docs.python.org/3/library/profile.html#pstats.Stats.sort_stats
pr.disable()
        

Context manager profiler

from contextlib import contextmanager

@contextmanager
def profile_block(print_top_n=10):
    pr = cProfile.Profile()
    pr.enable()

    yield

    pr.disable()

    s = io.StringIO()
    ps = pstats.Stats(pr, stream=s).sort_stats('cumtime')
    ps.print_stats(print_top_n)
    print(s.getvalue())
        

Proper Way To Time Functions

import timeit
timeit.timeit('func_to_time()',globals=globals(),number=1000)
        

Profiling Snippets

import time

# simple timer of function
def t(func, *args, **kwargs):
    t0 = time.time()
    func(*args, **kwargs)
    return time.time() - t0

# average time over multiple iterations
def ti(func, *args, **kwargs):
    iterations = 100000
    sum_ = 0
    for i in range(iterations):
        sum_ += t(func, *args, **kwargs)
    return sum_ / iterations