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)
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?