[Numpy-discussion] Timings for various round functions
Sasha
ndarray at mac.com
Wed Feb 22 22:06:07 EST 2006
C99 defines three functions round, rint and nearbyint that are nearly
identical. The only difference is in setting the inexact flag and
respecting the rounding mode. Nevertheless, these functions differ
significantly in their performance. I've wraped these functions into
ufuncs and go the following timings:
> python -m timeit -s "from numpy import array, round, rint, nearbyint; x = array([1.5]*10000)" "round(x)"
1000 loops, best of 3: 257 usec per loop
> python -m timeit -s "from numpy import array, round, rint, nearbyint; x = array([1.5]*10000)" "nearbyint(x)"
1000 loops, best of 3: 654 usec per loop
> python -m timeit -s "from numpy import array, round, rint, nearbyint; x = array([1.5]*10000)" "rint(x)"
10000 loops, best of 3: 103 usec per loop
Similarly for single precision:
> python -m timeit -s "from numpy import array, round, rint, nearbyint; x = array([1.5]*10000,dtype='f')" "round(x)"
10000 loops, best of 3: 182 usec per loop
> python -m timeit -s "from numpy import array, round, rint, nearbyint; x = array([1.5]*10000,dtype='f')" "nearbyint(x)"
1000 loops, best of 3: 606 usec per loop
> python -m timeit -s "from numpy import array, round, rint, nearbyint; x = array([1.5]*10000,dtype='f')" "rint(x)"
10000 loops, best of 3: 85.5 usec per loop
Obviously, I will use rint in my ndarray.round implementation,
however, it may be useful to provide all three as ufuncs.
The only question is what name to use for round?
1) round (may lead to confusion with ndarray.round or built-in round)
2) roundint (too similar to rint)
3) round0 (ugly)
Any suggestions?
Another C99 function that may be worth including is "trunc". Any
objections to adding it as a ufunc?
More information about the NumPy-Discussion
mailing list