hi there serdar jaeger lal pierre for infoworld at idg in this episode of smart python i'll be
introducing you to numba a library for writing numerical code in python that allows tremendous
speedups by way of a just-in-time compiler or jit python is known for being convenient to work with
but also slow that is slow compared to languages like c c plus plus or fortran python's slowest
when dealing with pure math operations because it's an interpreted language with a lot of dynamic
object behaviors it's not really optimized for math c c plus plus and fortran are optimized for
such things but at the cost of being less friendly to work with it's a trade-off now over the years
python's gained various mechanisms for doing math more quickly and efficiently i've covered a few of
them in this video series there are links in the video description to my other videos on numpy and
cython two common ways to accelerate python math performance with numpy you use libraries written
in c plus and fortran which are wrapped with python with cython you use a superset of python
syntax to transform python incrementally into c both of these approaches can yield big speedups
for common operations but number which i'm talking about today takes a completely different approach
it takes in a function that you've written in regular python and it uses a technology called
a just-in-time compiler or a jit to compile that function to machine code the compilation process
happens right when you run the program so there's no separate step to compile the code as there is
with cython and because you can write your own code you aren't limited to using numpy's metaphors
and libraries for how to handle things although you can integrate numpy and number if you need to
let's begin with a simple example this is a script that uses the monte carlo method to calculate pi
it's a very slow and inefficient way to calculate pi but it's a good way to demonstrate how number
can be used to speed things up we're going to run our monte carlo calculation in two iterations
of 10 million samples each the first time around we're just going to run it as pure python
the second time we're going to run a number compiled version
the first pass the pure python version takes about four seconds to run on this system
but the number version the exact same code just transformed through numbers jit runs about 15
times faster there's a small single time overhead cost associated with compiling the function
but it's offset massively by the speed gains and most importantly we've had
to make almost no modifications to our code all we've had to do is add the jit decorator
something else worth pointing out is that number has different strategies for compiling python code
the decorator takes an option no python and when set to true number will try to compile
the code in question so that it makes no use of the python interpreter if this is at all possible
this means the code will be maximally optimized for speed
but if number can't optimize for speed it will fall back to using the python interpreter where it
has to but at the cost of the program not being as fast as it could be number calls this object mode
there is still some speed up but nowhere near what there could be a no python mode
here is another example of number at work this is an implementation of something i've
used before as a demo of accelerated computation in python conway's game of life this is a simple
mathematical game that simulates the life of a colony of cells according to a set of rules
implementations of conway's life are always fun ways to test computational performance in
a language now i have two versions of the script one is written in pure python and the other is
accelerated with number the pure python version runs respectively fast it renders each generation
in about four or one thousandth of a second although the playing field is not very large
but the number version is much faster now when we run the first cycle of the cell generation code in
the number version you'll notice there's a slight pause as the jit compiler kicks in and compiles
the function but the number version's actual runtime for the function the compiled function
is about 80 times faster than its pure python counterpart and you can see this from how much
less time it takes to render each generation from that point forward and again we've accomplished
all of this with only the most minimal changes to the code the only thing we've had to do is add the
jit decorator and in this case change how the data areas that we created to hold the playing field
are passed to the jitted function this is because number has some small restrictions in terms of
what it can compile effectively it will fall back to unoptimized python code if it has no choice
but here we're making sure that it does not have to do that also because the code that we have here
is just numerical manipulation with matrices it's a prime candidate for optimization with
number there's no waiting on disk or network io which can't really be sped up with a faster
language anyway and finally there's relatively little dependence or no dependence at all on
the python runtime itself so that's our quick introduction to number if you liked it please
leave a comment below don't forget to subscribe to the idg tech talk channel on youtube and for more
smart python and more dev serdar generally be sure to follow me on facebook youtube and infoworld.com