0

I am searching for ways to get a better python performance. For example: fib.py

import datetime

def fibonacci(n):
 a,b = 1,1
 for i in range(n-1):
  a,b = b,a+b
 return a

t1 = datetime.datetime.now()
fibonacci(590000)
t2 =  datetime.datetime.now()

t=(t2-t1).total_seconds()

print("Time:",t)

took 4.337s.
I also wrote a extension module with a setup.py file which generates a fib.pyd file which can then be included. (Compiled with distutils)

from distutils.core import setup
from Cython.Build import cythonize
setup (ext_modules = cythonize("fib.pyx"))

It took 4.275s.
Am I doing it wrong, or why is Cython not improving the runtime? (Numba took 1.03s)
Any help/similar experience is appreciated.

J.To
  • 251
  • 1
  • 4
  • 10
  • The answers to [this question](http://stackoverflow.com/questions/4264229/no-speed-gains-from-cython) give good background on what Cython actually does and what you need to do to get speed improvements. (I'm almost tempted to mark it as a duplicate, but a bit undecided) – DavidW Aug 10 '16 at 12:10
  • If you're worried about performance on 2.7 use `xrange` instead of `range`. – Peter Wood Aug 10 '16 at 12:16
  • *"I also wrote a extension module"* and without that no one can really answer your question. Edit your question and supply it. – Dimitris Fasarakis Hilliard Aug 10 '16 at 19:05

0 Answers0