Small numpy arrays are very similar to Python scalars but numpy incurs a fair amount of extra overhead for simple operations. For large arrays this doesn't matter, but for code that manipulates a lot of small pieces of data, it can be a serious bottleneck.
For example
For example
In [1]: x = 1.0 In [2]: numpy_x = np.asarray(x) In [3]: timeit x + x 10000000 loops, best of 3: 61 ns per loop In [4]: timeit numpy_x + numpy_x 1000000 loops, best of 3: 1.66 us per loop
This project involved
- profiling simple operations like the above
- determining possible bottlenecks
- devising improved algorithms to solve them, with the goal of getting the numpy time as close as possible to the Python time.
Profiling tools
The very first objective to find bottleneck is profiling for time or space. During project I have used few tools for profiling and visualizing data of numpy execution flow.
Google profiling tool
Setting up Gperftools
Following are the steps used to setup python C level profiler on Ubuntu 13.04. (For any other system, options see [1])
- Make sure to build it from source. Clone svn repository from http://gperftools.googlecode.com/svn/trunk/
- In order to build gperftools checked out from subversion repository you need to have autoconf, automake and libtool installed.
- First, run ./autogen.sh script which generate ./configure and other files. Then run ./configure
- 'make check', to run any self-tests that come with the package. Check is optional but recommended to use
- After all test gets passed, type 'sudo make install' to install the programs and any data files and documentation.