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

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Feb 21 06:52:16 EST 2006


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