On Sep 29, 2009, at 10:05 AM, Sturla Molden wrote:

> Chris Colbert skrev:
>> No, the python ** gets translated to a pow statement by cython.
>>
>> I think the issue is that for some reason, i'm getting stuck in the
>> gcc slow pow function....
>>
>> if i let e2 and e1 be 1 and replace f**2 (which would call pow)  
>> with f*f,
>>
>> my execution time drops to this:
>> 10000 loops, best of 3: 108 µs per loop
>>
>> over 6x improvement just by avoid a few measly pow statements...
>> anyone know why i'm stuck in slowpow?
>>
> pow() is "slow" because it is a general function that can compute any
> power, including pow(x, -231436.74638746238746). It is not  
> restricted to
> integers only. Therefore,
>
> cdef inline double pow0(double x):
>     return 1
>
> cdef inline double pow1(double x):
>     return x
>
> cdef inline double pow2(double x):
>     return x*x
>
> cdef inline double pow3(double x):
>     return x*x*x
>
> is (often) much faster than pow(x,0), pow(x,1), pow(x,2), and pow(x, 
> 3).

Some C compilers will unroll pow(x,2) for you, but I'm surprised they  
don't go further (at least in my experiments). This unrolling for  
small constant integer powers may actually be worth adding to Cython  
itself.

>>>>> and within that loop it is these statements that take the bulk  
>>>>> of the time:
>>>>>
>>>>> F = ((f1**2)**(1/e2) + (f2**2)**(1/e2))**(e2/e1) + (f3**2)**(1/e1)

Is there a reason you're doing ((f2**2)**(1/e2))**(e2/e1) rather than  
f2**(2/e1) or even (f2*f2)**(1/e1)?

- Robert

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to