[Numpy-discussion] Interesting timing results

Travis Oliphant oliphant.travis at ieee.org
Thu Jan 19 13:38:11 EST 2006


Sasha wrote:

>On 1/17/06, Travis Oliphant <oliphant.travis at ieee.org> wrote:
>  
>
>>...                        Currently all array scalar math goes through the
>>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 = bool_(0)" "x & x"
>>    
>>
>100000 loops, best of 3: 3.85 usec per loop
>
>Now:
>  
>
>>python -m timeit -s "from numpy import bool_; x = 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 = 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 = 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_
>  
>
+1

>2. true and false
>  
>
-1

>The second issue is whether to add new numbers to _ARRAY_API or add a
>separate _ARRAY_SCALAR_API .
>  
>
No separate _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 
>(bool_(0) | x)  can become a new array that shares data with x.
>  
>
Other operations with scalars return a new array.  The fact that this 
would be different would probably be a bad thing in the end.    So, I 
vote for returning a new copy of the data...

-Travis







More information about the NumPy-Discussion mailing list