Numpy slow at vector cross product?

Steve D'Aprano steve+python at pearwood.info
Sun Nov 20 21:48:01 EST 2016


On Mon, 21 Nov 2016 07:46 am, 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)

[...]
> $ python vector_cross.py
> Numpy, 100000 loops: 2.5 seconds
> Calc,  100000 loops: 0.13 seconds
> 
> 
> Did I do something wrong, or is numpy slow at this?

I can confirm similar results.

However, your code is not a great way of timing code. Timing code is *very*
difficult, and can be effected by many things, such as external processes,
CPU caches, even the function you use for getting the time. Much of the
time you are timing here will be in creating the range(loops) list,
especially if loops is big.

The best way to time small snippets of code is to use the timeit module.
Open a terminal or shell (*not* the Python interactive interpreter, the
operating system's shell: you should expect a $ or % prompt) and run timeit
from that. Copy and paste the following two commands into your shell
prompt:


python2.7 -m timeit --repeat 5 -s "import numpy as np" \
-s "x = np.array([1, 2, 3])" -s "y = np.array([4, 5, 6])" \
-- "np.cross(x, y)"


python2.7 -m timeit --repeat 5 -s "x = [1, 2, 3]" \
-s "y = [4, 5, 6]" -s "z = [0, 0, 0]" \
-- "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]"


The results I get are:

10000 loops, best of 5: 30 usec per loop

1000000 loops, best of 5: 1.23 usec per loop


So on my machine, np.cross() is about 25 times slower than multiplying by
hand.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list