Poor numpy.cross () performance

I conducted performance testing to improve the performance of the amateur project that I am writing. This is a very intense crunching app, so I played with Numpy as a way to improve computing performance.

However, the result of the following performance tests was rather unexpected ....

Source code for testing (Updated using test cases for download and batch presentation)

import timeit numpySetup = """ import numpy left = numpy.array([1.0,0.0,0.0]) right = numpy.array([0.0,1.0,0.0]) """ hoistSetup = numpySetup +'hoist = numpy.cross\n' pythonSetup = """ left = [1.0,0.0,0.0] right = [0.0,1.0,0.0] """ numpyBatchSetup = """ import numpy l = numpy.array([1.0,0.0,0.0]) left = numpy.array([l]*10000) r = numpy.array([0.0,1.0,0.0]) right = numpy.array([r]*10000) """ pythonCrossCode = """ x = ((left[1] * right[2]) - (left[2] * right[1])) y = ((left[2] * right[0]) - (left[0] * right[2])) z = ((left[0] * right[1]) - (left[1] * right[0])) """ pythonCross = timeit.Timer(pythonCrossCode, pythonSetup) numpyCross = timeit.Timer ('numpy.cross(left, right)' , numpySetup) hybridCross = timeit.Timer(pythonCrossCode, numpySetup) hoistCross = timeit.Timer('hoist(left, right)', hoistSetup) batchCross = timeit.Timer('numpy.cross(left, right)', numpyBatchSetup) print 'Python Cross Product : %4.6f ' % pythonCross.timeit(1000000) print 'Numpy Cross Product : %4.6f ' % numpyCross.timeit(1000000) print 'Hybrid Cross Product : %4.6f ' % hybridCross.timeit(1000000) print 'Hoist Cross Product : %4.6f ' % hoistCross.timeit(1000000) # 100 batches of 10000 each is equivalent to 1000000 print 'Batch Cross Product : %4.6f ' % batchCross.timeit(100) 

Original Results

 Python Cross Product : 0.754945 Numpy Cross Product : 20.752983 Hybrid Cross Product : 4.467417 

End results

 Python Cross Product : 0.894334 Numpy Cross Product : 21.099040 Hybrid Cross Product : 4.467194 Hoist Cross Product : 20.896225 Batch Cross Product : 0.262964 

Needless to say, this was not the result that I expected. The clean version of Python is nearly 30 times faster than Numpy. The performance in other tests was better than the Python equivalent (this was the expected result).

So, I have two related questions:

  • Can anyone explain why NumPy works so bad in this case?
  • Is there something I can do to fix this?
+4
source share
4 answers

Try this with large arrays. I think that only the cost of calling numpy methods here overwhelms the simple access to multiple lists required by the Python version. If you are dealing with larger arrays, I think you will see big winnings for numpy .

+6
source

You can see the source code yourself: http://www.google.com/codesearch/p?hl=en#5mAq98l-MUw/trunk/dnumpy/numpy/core/numeric.py&q=cross%20package:numpy&sa=N&cd=1&ct = rc

numpy.cross just handles a lot of cases and makes some extra copies.

In general, numpy will be fast enough for slow things like matrix multiplication or inversion, but operations with such small vectors have a lot of overhead.

+5
source

To reduce the numpy call overhead, you can try using cython as an intermediate element to call numpy functions.

See fast numerical computations with Cython for details (SciPy 2009) .

+1
source

Great post! I think the comparison is not really fair. The Batch Cross Product gives an array containing the cross products of all vectors, while the Python Cross Product gives one vector at a time. If you need to calculate all cross-products at once, of course, the package is better, but if you need to calculate each cross-product separately, you must include the overhead of accessing the array. In addition, if the cross-product, if the function of the previous cross-product must be changed, the batch implementation must be changed.

0
source

All Articles