# [Numpy-discussion] Optimizing mean(axis=0) on a 3D array

Francesc Altet faltet at carabos.com
Sat Aug 26 16:22:00 CDT 2006

```A Dissabte 26 Agost 2006 12:26, Travis Oliphant va escriure:
> If frameis is 1-D, then you should be able to use
>
> temp = data.take(frameis,axis=0)
>
> for the first step.   This can be quite a bit faster (and is a big
> reason why take is still around).   There are several reasons for this
> (one of which is that index checking is done over the entire list when
> using indexing).

Well, some days ago I've stumbled on this as well. NumPy manual says
that .take() is usually faster than fancy indexing, but my timings shows that
this is no longer true in recent versions of NumPy:

In [56]: Timer("b.take(a)","import numpy; a=numpy.arange(999,-1,-1,
dtype='l');b=a[:]").repeat(3,1000)
Out[56]: [0.28740906715393066, 0.20345211029052734, 0.20371079444885254]

In [57]: Timer("b[a]","import numpy; a=numpy.arange(999,-1,-1,
dtype='l');b=a[:]").repeat(3,1000)
Out[57]: [0.20807695388793945, 0.11684703826904297, 0.11686491966247559]

I've done some profiling on this and it seems that take is using C memmove
call so as to copy the data, and this is *very* slow, at least in my platform
(Linux on Intel). On its hand, fancy indexing seems to use an iterator and
copying the elements one-by-one seems faster. I'd say that replacing memmove
by memcpy would make .take() much faster.

Regards,

--
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
"-"

```