Skip to content

Instantly share code, notes, and snippets.

@nvdv
nvdv / stat_prof_full.py
Created October 5, 2016 09:51
How profilers work - statistical approach - full listing
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
@nvdv
nvdv / det_prof.py
Last active October 5, 2016 09:48
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
@nvdv
nvdv / summary_stat.py
Last active October 5, 2016 09:50
How profilers work - stat
def print_summary(stats, total_time):
total_samples = sum(stats.values())
print('Total time: %f s' % total_time)
print('Total samples: %s' % total_samples)
for curr_stack, sample_count in stats.items():
num_samples = sample_count
children = [
stack for stack in stats
if len(stack) > len(curr_stack) and curr_stack in stack]
@nvdv
nvdv / stat_prof.py
Created October 4, 2016 10:27
How profilers work - statistical profilers
import defaultdict
import time
import signal
SAMPLE_INTERVAL = 0.001 # seconds
def sample_stack(signum, frame):
stack = []
while frame:
stack.append(frame.f_code.co_name)
@nvdv
nvdv / summary_det.py
Last active October 5, 2016 09:48
How profilers work - summary deterministic
def print_summary(stats, total_time):
print('Total time: %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))
@nvdv
nvdv / stats_running.py
Last active October 4, 2016 10:02
How profilers work - running with stats
stats = defaultdict(lambda: defaultdict(float))
start_time = time.time()
primes = get_primes(1000)
total_time = time.time() - start_time
print("Total time is %f s" % total_time)
@nvdv
nvdv / wrapping2.py
Created October 4, 2016 09:36
How profilers work - @profile_stats wrapping
@profile_stats
def is_prime(n):
...
@profile_stats
def sum_of_digits(n):
...
@profile_stats
def get_primes(n):
@nvdv
nvdv / profile_stats.py
Last active October 4, 2016 09:37
How profilers work - collecting stats with decorator
def profile_stats(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func_name = func.__name__
# We need this small hack to keep things simple.
caller_name = sys._getframe(1).f_code.co_name
result = func(*args, **kwargs)
stats[func_name][caller_name] += time.time() - start_time
return result
return wrapper
@nvdv
nvdv / wrapping1.py
Created October 4, 2016 09:15
How profilers work - @Profile wrapping
@profile
def is_prime(n):
...
@profile
def sum_of_digits(n):
...
@profile
def get_primes(n):
@nvdv
nvdv / prof_decorator.py
Last active October 4, 2016 09:20
How profilers work - profiling decorator
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