sign() much slower in python than matlab? -
i have function in python takes sign of array (75,150), example. i'm coming matlab , time execution looks more or less same less function. i'm wondering if sign() works , know alternative same.
thx,
i can't tell if faster or slower matlab, since have no idea numbers you're seeing there (you provided no quantitative data @ all). however, far alternatives go:
import numpy np = np.random.randn(75, 150) asign = np.sign(a)
testing using %timeit
in ipython
:
in [15]: %timeit np.sign(a) 10000 loops, best of 3: 180 µs per loop
because loop on array (and happens inside it) implemented in optimized c code rather generic python code, tends order of magnitude faster—in same ballpark matlab.
comparing exact same code numpy vectorized operation vs. python loop:
in [276]: %timeit [np.sign(x) x in a] 1000 loops, best of 3: 276 per loop in [277]: %timeit np.sign(a) 10000 loops, best of 3: 63.1 per loop
so, 4x fast here. (but a
pretty small here.)
Comments
Post a Comment