[SciPy-user] Vectorize vs Map
Travis Oliphant
oliphant@ee.byu....
Fri Mar 16 11:13:45 CDT 2007
lorenzo bolla wrote:
> here is a simple timing on my machine.
>
> ---------------------------------------------------
>
> import scipy as S
> from timeit import Timer
>
> def f(x):
> return S.sqrt(S.absolute(x)**2)
>
> x = S.array(S.rand(1000))
> fv = S.vectorize(f)
> fu = S.frompyfunc(f,1,1)
>
> def test():
> #f(x)
> #fv(x)
> fu(x)
>
> if __name__ == '__main__':
> t = Timer('test()', 'from __main__ import test')
> n = 100
> print "%.2f usec/pass" % (1e6*t.timeit(number=n)/n)
>
>
> ---------------------------------------------------
>
> I get:
> 229.84 usec/pass for f(x)
> 119410.40 usec/pass for fv(x)
> 114513.80 usec/pass for fu(x)
>
> vectorize and frompyfunc create functions roughly 500 times slower
> than the one using ndarrays arithmetics (even if it cannot operate on
> lists, just ndarrays).
>
There is nothing new here. It is not surprising at all. From pyfunc
creates a ufunc out of a python function. The python function is
called at each element-by-element calculation. In this case, the
element-by-element calculation is also using ufuncs to compute the
result (ufuncs are a very slow way to compute a single scalar operation).
Because a Python function is called, the ufunc uses object arrays as well.
All of these intermediate things means the result will be much slower.
Vectorize and frompyfunc are only useful when you want a quick ufunc
that you can't figure out how to vectorize yourself.
If you can vectorize it, it is always better to do it than to use
vectorize.
-Travis
More information about the SciPy-user
mailing list