Skip to content

Instantly share code, notes, and snippets.

@nvdv
Created October 5, 2016 09:51
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/e7aa07272d928273bca811895e21f5ba to your computer and use it in GitHub Desktop.
Save nvdv/e7aa07272d928273bca811895e21f5ba to your computer and use it in GitHub Desktop.
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
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
import signal
from collections import defaultdict
SAMPLE_INTERVAL = 0.001
stats = defaultdict(int)
def sample_stack(signum, frame):
stack = []
while frame:
stack.append(frame.f_code.co_name)
frame = frame.f_back
stack_name = '-'.join(reversed(stack))
stats[stack_name] += 1
signal.setitimer(signal.ITIMER_PROF, SAMPLE_INTERVAL)
signal.signal(signal.SIGPROF, sample_stack)
signal.setitimer(signal.ITIMER_PROF, SAMPLE_INTERVAL)
start_time = time.time()
primes = get_primes(1000)
end_time = time.time()
signal.setitimer(signal.ITIMER_PROF, 0)
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]
# Number of samples for function is number of samples caught by
# the function plus sum of samples of children.
if children:
num_samples += sum(stats[child] for child in children)
sample_percent= 100.0 * num_samples / total_samples
print('Function: %s, sample count: %d, percentage: %f %%' % (
curr_stack, num_samples, sample_percent))
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