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 |
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 |
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 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] |
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
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) |
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 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)) |
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
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) |
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
@profile_stats | |
def is_prime(n): | |
... | |
@profile_stats | |
def sum_of_digits(n): | |
... | |
@profile_stats | |
def get_primes(n): |
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 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 |
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
@profile | |
def is_prime(n): | |
... | |
@profile | |
def sum_of_digits(n): | |
... | |
@profile | |
def get_primes(n): |
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 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 |
NewerOlder