Last active
October 5, 2016 09:48
-
-
Save nvdv/a210dc5d1a3f797cdb4be806ba507f1a to your computer and use it in GitHub Desktop.
How profilers work - deterministic approach
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
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