Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 1015 Bytes

README.md

File metadata and controls

36 lines (25 loc) · 1015 Bytes

Using C-functions

Fibonacci numbers are a sequence of integers defined by the recurrence relation

Fn = Fn-1 + Fn-2

with the initial values F0=0, F1=1.

The module fib.py contains a function fibonacci(n) that calculates recursively Fn. The function can be used e.g. as

from fib import fibonacci

fibonacci(30)

Make a Cython version of the module, and investigate how adding type information and making fibonacci a C-function affects performance (hint: function needs to be called both from Python and C). Use timeit for performance measurements, either from command line

$ python3 -m timeit -s "from fib import fibonacci" "fibonacci(30)"

or within IPython

In []: %timeit fibonacci(30)

Note: this recursive algorithm is very inefficient way of calculating Fibonacci numbers and pure Python implemention of better algorithm outperforms Cython implementation drastically.