# [SciPy-user] Vectorize vs Map

lorenzo bolla lbolla@gmail....
Fri Mar 16 04:37:21 CDT 2007

```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).

lorenzo.

On 3/15/07, Robert Kern <robert.kern@gmail.com> wrote:
>
> Fernando Perez wrote:
> > On 3/15/07, Robert Kern <robert.kern@gmail.com> wrote:
> >> Lorenzo Isella wrote:
> >>> Dear All,
> >>> Probably another newbie question: I like quite a lot the vectorize()
> >>> command which allows me to skip iterations on functions, but the map()
> >>> command on a list performs a similar task if I am not mistaken.
> >>> Is there any reason to favour one above the other or is it just a
> >>> matter of taste?
> >> vectorize() takes a Python function and turns it into a ufunc. ufuncs
> do a lot
> >> more than map() does. They can take multidimensional arrays. n-ary
> ufuncs can
> >> take multiple inputs and broadcast them against each other. ufuncs have
> methods
> >> like .inner() and .reduce() which are quite powerful.
> >
> > Mmh, isn't that what 'frompyfunc' does instead?  vectorize doesn't
> > seem to produce a true ufunc.  Perhaps I'm just misunderstanding
> > something:
>
> No, just my poor memory. I don't use either.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma
> that is made terrible by our own mad attempt to interpret it as though it
> an underlying truth."
> -- Umberto Eco
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070316/769f01d2/attachment-0001.html
```