Skip to content

Performance of Fibonnaci compare to Python

There's a 4 second, unjustified, difference in performance between the following Python fibonacci implementation and it's equivalent Haskell implementation (though the output core seems to be just fine!).

-----PYTHON------
def fib(n):
    one = 0
    two = 1
    for i in range(0,n):
        temp = two
        two = one + two
        one = temp
    return one

if __name__ == "__main__":
    print fib(1000000);
----HASKELL-------
{-# LANGUAGE BangPatterns #-}

fib :: Int -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib' 0 1 2 where
    fib' _ y n' | n' > n = y
    fib' !x !y !n' = fib' y (x+y) (n'+1)

main = fib 1000000 `seq` return ()
Edited by Simon Marlow
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information