[Numpy-discussion] optimizing power() for complex and real cases

David M. Cooke cookedm at physics.mcmaster.ca
Thu Feb 23 22:03:02 CST 2006


cookedm at physics.mcmaster.ca (David M. Cooke) writes:

> Hmm, scalar division and multiplication could both be speed up:
>
> In [36]: a = arange(10000, dtype=float)
> In [37]: %time for i in xrange(100000): a * 1.0
> CPU times: user 3.30 s, sys: 0.00 s, total: 3.30 s
> Wall time: 3.39
> In [38]: b = array([1.])
> In [39]: %time for i in xrange(100000): a * b
> CPU times: user 2.63 s, sys: 0.00 s, total: 2.63 s
> Wall time: 2.71
>
> The differences in times are probably due to creating an array to hold
> 1.0.
>
> When I have time, I'll look at the ufunc machinery. Since ufuncs are
> just passed pointers to data and strides, there's no reason (besides
> increasing complexity ;-) to build an ndarray object for scalars.

I've had a look: basically, if you pass 1.0, say, to a ufunc, it ends
up going through PyArray_FromAny. This did checks for the array
interface first (looking for attributes __array_shape__, __array_typestr__,
__array_struct__, __array__, etc.). These would always fail for Python
scalar types.

I special-cased Python scalar types (bool, int, long, float, complex)
in PyArray_FromAny so they are checked for first. This *does* have the
side effect that if you have a subclass of one of these that does
define the array interface, that interface is not used.

If anybody's worried about that, well...tough :-) Give me a reasonable
test case for subclassing a Python scalar and adding the array interface.

This gives me the times

In [1]: a = arange(10000, dtype=float)
In [2]: %time for i in xrange(100000): a * 1.0
CPU times: user 2.76 s, sys: 0.00 s, total: 2.76 s
Wall time: 2.85
In [3]: b = array([1.])
In [4]: %time for i in xrange(100000): a * b
CPU times: user 2.69 s, sys: 0.00 s, total: 2.69 s
Wall time: 2.76

The overhead of a * 1.0 is 3% compared to a * b here, as opposed to
25% in my last set of numbers.

[for those jumping in, this is all still in the power_optimization branch]

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca




More information about the Numpy-discussion mailing list