he profile module provides APIs for collecting and analyzing statistics about how Python source consumes processor resources.
In [1]:
import profile
def fib(n):
if n <2:
return n
else:
return fib(n-2) + fib(n-1)
def fib_seq(n):
seq = []
if n > 0:
seq.extend(fib_seq(n-1))
seq.append(fib(n))
return seq
profile.run('print(fib_seq(20));print()')
In [2]:
import functools
import profile
@functools.lru_cache(maxsize=None)
def fib(n):
# from literateprograms.org
# http://bit.ly/hlOQ5m
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
def fib_seq(n):
seq = []
if n > 0:
seq.extend(fib_seq(n - 1))
seq.append(fib(n))
return seq
profile.run('print(fib_seq(20));print()')
In [3]:
profile.runctx('print(fib_seq(n)); print()', globals(), {'n':20})
In [4]:
import cProfile as profile
import pstats
# Create 5 set of stats
for i in range(5):
filename = 'profile_stats_{}.stats'.format(i)
profile.run('print({}, fib_seq(20))'.format(i), filename)
# Read all 5 stats files into a single object
stats = pstats.Stats('profile_stats_0.stats')
for i in range(1, 5):
stats.add('profile_stats_{}.stats'.format(i))
# Clean up filenames for the report
stats.strip_dirs()
# Sort the statistics by the cumulative time spent
# in the function
stats.sort_stats('cumulative')
stats.print_stats()
Out[4]: