At the command line:
!time python slow_functions.py
import slow_functions
With the time
module:
import time
t1 = time.time()
result = slow_functions.main()
t2 = time.time()
print 'slow_functions.main took {} seconds'.format(t2 - t1)
IPython magic: %run
%run -t slow_functions.py
Ipython magic: %timeit
%timeit slow_functions.main()
import numpy as np
from numpy import interp
from scipy.interpolate import interp1d
x = np.linspace(0, 2*np.pi, 10)
y = np.sin(x)
xvals = np.linspace(0, 2*np.pi, 50)
#scipy
f = interp1d(x, y)
%timeit f(xvals)
# numpy
%timeit interp(xvals, x, y)
scipy_vals = f(xvals)
numpy_vals = interp(xvals, x, y)
print np.allclose(scipy_vals, numpy_vals)
At the command line:
!python -m cProfile slow_functions.py
Viewing with pstats
!python -m cProfile -o slow_functions.prof slow_functions.py
import pstats
stats = pstats.Stats('slow_functions.prof')
stats.print_stats()
stats.sort_stats('cum').print_stats()
stats.sort_stats('time').print_stats()
stats.sort_stats('time').print_stats(3)
stats.sort_stats('cum').print_stats(r'func\d')
stats.sort_stats('time').print_stats(r'func\d', 3)
More IPython magic: %prun
%prun slow_functions.main()
%prun -D slow_functions_main.prof slow_functions.main()
stats = %prun -q -r f(xvals)
# work around a bug in IPython
import sys
stats.stream = sys.stdout
stats.sort_stats('time').print_stats(10)
%%prun
f(xvals)
interp(xvals, x, y)
line_profiler
with IPython magic
%load_ext line_profiler
%lprun -f slow_functions.main slow_functions.main()
%lprun -f slow_functions.func5 slow_functions.main()
%lprun -f f.__call__ f(xvals)
%prun -q -D scipy_interp.prof f(xvals)
%prun -q -D numpy_interp.prof interp(xvals, x, y)