John Parejko, Phil Marshall and Your Name Here>
This notebook demonstrates some ways to make your python code go faster.
Because how can you optimize something if you haven't first evaluated it?
Because you probably own more than one CPU.
In [2]:
import numpy as np
In [5]:
x = np.random.randn(1000)
In [6]:
%timeit np.power(x,2)
In [8]:
%timeit x**2
In [ ]:
In [11]:
import cProfile
import pstats
In [23]:
def square(x):
for k in range(1000):
sq = np.power(x,2)
sq = x**2
sq = x*x
return
In [24]:
log = 'square.profile'
cProfile.run('square(x)',filename=log)
stats = pstats.Stats(log)
stats.strip_dirs()
stats.sort_stats('cumtime').print_stats(20)
In [29]:
# OK - so all the time is being taken by the function "square".
# We need to re-write with the lines separated into functions -
# which is a better way to code anyway.
In [30]:
def bettersquare(x):
def powersquare(x):
return np.power(x,2)
def justsquare(x):
return x**2
def selfmultiply(x):
return x*x
for k in range(1000):
sq = powersquare(x)
sq = justsquare(x)
sq = selfmultiply(x)
return
In [31]:
log = 'bettersquare.profile'
cProfile.run('bettersquare(x)',filename=log)
stats = pstats.Stats(log)
stats.strip_dirs()
stats.sort_stats('cumtime').print_stats(20)
Out[31]:
In [ ]:
# Much better - you can see the cumulative time spent in each function.
In [28]:
# We could also try the line profiler, from rkern on GitHub.
!pip install --upgrade line_profiler
We could also run the line_profiler from the command line...
Which means the square function needs writing out to a file...
Can we do this from this notebook?
Something of a last resort: don't go to cython unless you know it's going to help...
We could also replace simple lines of math with the equivalent line of c...
cython -a file.pyx
makes file.c, but also file.html. The html file shows you the lines that were unwrapped into c.
Can we demo this from this notebook? Hmm.
In [ ]: