|
From: Sasha <nd...@ma...> - 2006-01-19 20:56:44
|
On 1/17/06, Travis Oliphant <oli...@ie...> wrote: > ... Currently all array scalar math goes through t= he > entire ufunc machinery. This is clearly sub-optimal. It is the reason > for the scalarmath module that I've started in the src directory. > Eventually, we should be able to have scalar math as fast as Python > scalars. I have implemented "nonzero", &, | and ^ for scalar bools. (See http://projects.scipy.org/scipy/numpy/changeset/1946). Here are the timings: Before: > python -m timeit -s "from numpy import bool_; x =3D bool_(0)" "x & x" 100000 loops, best of 3: 3.85 usec per loop Now: > python -m timeit -s "from numpy import bool_; x =3D bool_(0)" "x & x" 10000000 loops, best of 3: 0.174 usec per loop This is close to python bool: > python -m timeit -s "x =3D bool(0)" "x & x" 10000000 loops, best of 3: 0.142 usec per loop and faster than python int: > python -m timeit -s "from numpy import bool_; x =3D 0" "x & x" 10000000 loops, best of 3: 0.199 usec per loop But it is in fact all within the timing error now. I did not put it in the scalarmath module for two reasons: first, scalarmath is not hooked to numpy yet and second because C-API does not provide access to scalar bools yet. I have posted a proposal for C-API changes and did not hear any opposition (well, no support either), so I will implement that soon. There are a few issues with the new APIs that I proposed. First is a simple one: I proposed to expose boolean scalars as named constants to Python, the question is how to call them. 1. True_ and False_ 2. true and false The second issue is whether to add new numbers to _ARRAY_API or add a separate _ARRAY_SCALAR_API . I've only optimized scalar-scalar case in binary ops and fall back to old for everything else. I would like to optimize scalar-array and array-scalar cases as well, but I wonder if it is apropriate to make "(bool_(0) | x) is x" true when x is an array. Alternatively=20 (bool_(0) | x) can become a new array that shares data with x. -- sasha |