Cython taking more time than regular Python

BartC bc at freeuk.com
Tue Sep 20 05:44:20 EDT 2016


On 20/09/2016 06:27, Stefan Behnel wrote:
> Peter Otten schrieb am 19.09.2016 um 14:55:
>> In [7]: %%cython
>> def omega(int n):
>>     cdef long i
>>     cdef long result = 0
>>     for i in range(n): result += i
>>     return result
>>    ...:
>>
>> In [8]: %timeit omega(100000)
>> 10000 loops, best of 3: 91.6 µs per loop
>
> Note that this is the worst benchmark ever. Any non-dump C compiler will
> happily apply Young Gauß and calculate the result in constant time.

Which 'non-dump' C compiler will actually do such an optimisation? (Just 
so I can steer clear of it; I don't want a compiler wasting /my/ time 
doing pointless analysis.)

This actually quite a good micro-benchmark precisely because that would 
be an unusual optimisation.

(With benchmarking you usually want to know how long it takes a program 
to perform a task, not how long it takes to *not* do it!)

-- 
Bartc



More information about the Python-list mailing list