[Tutor] Loop comparison

Stefan Behnel stefan_ml at behnel.de
Fri Apr 16 13:03:02 CEST 2010

Steven D'Aprano, 16.04.2010 12:00:
> On Fri, 16 Apr 2010 06:29:40 pm Alan Gauld wrote:
>> "Stefan Behnel"  wrote
>>>      import cython
>>>      @cython.locals(result=cython.longlong, i=cython.longlong)
>>>      def add():
>>>          result = 0
>>>          for i in xrange(1000000000):
>>>              result += i
>>>          return result
>>>      print add()
> [...]
>> Or is cython doing the precalculation optimisations you mentioned?
>> And if so when does it do them? Because surely, at some stage, it
>> still has to crank the numbers?
>> (We can of course do some fancy math to speed this particular
>> sum up since the result for any power of ten has a common pattern,
>> but I wouldn't expect the compiler optimiser to be that clever)
> No fancy maths needed, although I'd be amazed (in a good way) to learn
> that compiler compiler optimizers recognised this case! Are optimizing
> compilers really that clever these days?

If I was writing an optimising C compiler, I'd certainly put this in to 
show off in common benchmarks. ;-)


More information about the Tutor mailing list