[Numpy-discussion] A proposal to implement round in C Was: Rookie problems - Why is C-code much faster?

Mads Ipsen mpi at osc.kiku.dk
Wed Feb 22 02:27:02 CST 2006


On Tue, 21 Feb 2006, Sasha wrote:

> > python -m timeit -s "from numpy import array; x = array([1.5]*1000)" "(x+0.5).astype(int).astype(float)"
> 100000 loops, best of 3: 18.8 usec per loop
> > python -m timeit -s

just want to point out that the function

  foo(x) = (x+0.5).astype(int).astype(float)

is different from around. For

  x = array([1.2, 1.8])

it works but for

  x = array([-1.2, -1.8])

you get

  around(x) = array([-1., -2.])

whereas foo(x) gives

  foo(x) = array([0., -1.])

Using

  foo(x) =  where(greater(x,0),x+0.5,x-0.5).astype(int).astype(float)

will work.

// Mads







More information about the Numpy-discussion mailing list