[Numpy-discussion] Rookie problems - Why is C-code much faster?

Sasha ndarray at mac.com
Tue Feb 21 07:00:00 EST 2006


On the second thought, the difference between around and astype is not
surprising because around operates in terms of decimals.  Rather than
adding rint, I would suggest to make a special case decimals=0 use C
rint.

> On 2/21/06, Mads Ipsen <mpi at osc.kiku.dk> wrote:
> > I suggest that rint() is added as a ufunc or is there any concerns
> > here that I am not aware of?
>
> You might want to use astype(int).  On my system it is much faster than around:
>
> > python -m timeit -s "from numpy import array, around; x = array([1.5]*1000)" "around(x)"
> 10000 loops, best of 3: 176 usec per loop
> > python -m timeit -s "from numpy import array, around; x = array([1.5]*1000)" "x.astype(int)"
> 100000 loops, best of 3: 3.2 usec per loop
>
> the difference is too big to be explained by the fact that around
> allocates twice as much memory for the result.  In fact the following
> equivalent of rint is still very fast:
>
> > python -m timeit -s "from numpy import array, around; x = array([1.5]*1000)" "x.astype(int).astype(float)"
> 100000 loops, best of 3: 6.48 usec per loop
>




More information about the NumPy-Discussion mailing list