Skip to content

Instantly share code, notes, and snippets.

@nvdv
Last active October 5, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvdv/a210dc5d1a3f797cdb4be806ba507f1a to your computer and use it in GitHub Desktop.
Save nvdv/a210dc5d1a3f797cdb4be806ba507f1a to your computer and use it in GitHub Desktop.
How profilers work - deterministic approach
def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True
def sum_of_digits(n):
s = 0
while n:
s += n % 10
n //= 10
return s
def get_primes(n):
primes = []
candidate = 2
while len(primes) < n:
if is_prime(candidate) and sum_of_digits(candidate) % 2 == 0:
primes.append(candidate)
candidate += 1
return primes
import time
start_time = time.time()
primes = get_primes(1000)
total_time = time.time() - start_time
print("Total time is %f s" % total_time)
# Decorator approach
def profile(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
print("Function %s run time is %f s" % (
func.__name__, time.time() - start_time))
return result
return wrapper
# is_prime = profile(is_prime)
# get_n_primes = profile(get_primes)
# sum_of_digits = profile(sum_of_digits)
# primes = get_primes(1000)\
# Decorator with stats
def profile_stats(func):
def wrapper(*args, **kwargs):
start_time = time.time()
# We need this small hack to keep things simple.
caller = sys._getframe(1).f_code.co_name
result = func(*args, **kwargs)
stats[func.__name__][caller] += time.time() - start_time
return result
return wrapper
import sys
import time
from collections import defaultdict
is_prime = profile_stats(is_prime)
get_n_primes = profile_stats(get_primes)
sum_of_digits = profile_stats(sum_of_digits)
stats = defaultdict(lambda: defaultdict(float))
start_time = time.time()
primes = get_primes(1000)
end_time = time.time()
def print_summary(stats, total_time):
print('Total runtime: %f s' % total_time)
for f_name in stats:
func_time = 0
for caller in stats[f_name]:
func_time += stats[f_name][caller]
percentage = float(func_time) / total_time
print('Function: %s, caller: %s, function run time: %f s, percentage: %f %%' % (
f_name, caller, func_time, 100 * percentage))
print_summary(stats, end_time - start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment