Numpy slow at vector cross product?

BartC bc at freeuk.com
Sun Nov 20 20:38:47 EST 2016


On 20/11/2016 20:46, DFS wrote:
> import sys, time, numpy as np
> loops=int(sys.argv[1])
>
> x=np.array([1,2,3])
> y=np.array([4,5,6])
> start=time.clock()
> for i in range(loops):
>     np.cross(x,y)
> print "Numpy, %s loops: %.2g seconds" %(loops,time.clock()-start)
>
> x=[1,2,3]
> y=[4,5,6]
> z=[0,0,0]
> start=time.clock()
> for i in range(loops):
>     z[0]=x[1]*y[2]-x[2]*y[1];
>     z[1]=x[2]*y[0]-x[0]*y[2];
>     z[2]=x[0]*y[1]-x[1]*y[0];
> print "Calc, %s loops: %.2g seconds" %(loops,time.clock()-start)

I don't know why numpy is slow, but I can confirm similar results.

In fact if the workings are put into a function, then the difference is 
even more marked, with the normal calculation being 50% faster

Maybe numpy has extra overheads, and the arrays being operated on are 
very small, but even so, 30 times slower than CPython? (2.5 to 0.083 
seconds.)

-- 
Bartc







More information about the Python-list mailing list