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

Mads Ipsen mpi at osc.kiku.dk
Tue Feb 21 07:24:08 EST 2006


On Tue, 21 Feb 2006, Alexander Belopolsky wrote:

> 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;

Maybe I am wrong here, but around() and rint() is supposed to round to
the closest integer, i.e. for x = array([1.1, 1.8])

  around(x) = [1.0, 2.0]

whereas

  x.astype(int).astype(float) = [1.0, 1.0]

This particular property of around() as well as rint() is crucial for my
application.

// Mads




More information about the NumPy-Discussion mailing list