Opened 2 years ago

Closed 2 years ago

#6000 closed bug (worksforme)

Performance of Fibonnaci compare to Python

Reported by: guest Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.4.1
Keywords: python, performance Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Runtime performance bug Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description (last modified by simonmar)

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 ()

Attachments (2)

fibs.hs (204 bytes) - added by guest 2 years ago.
Haskell implementation
pyfib.py (191 bytes) - added by guest 2 years ago.
Python implementation

Download all attachments as: .zip

Change History (7)

Changed 2 years ago by guest

Haskell implementation

Changed 2 years ago by guest

Python implementation

comment:1 Changed 2 years ago by tibbe

I can't reproduce your results:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.4.1
$ ghc -O2 fibs.hs 
[1 of 1] Compiling Main             ( fibs.hs, fibs.o )
Linking fibs ...
$ time ./fibs 

real	0m6.715s
user	0m6.605s
sys	0m0.107s
$ time python pyfib.py
<snip>

real	0m13.701s
user	0m13.559s
sys	0m0.071s

comment:2 follow-up: Changed 2 years ago by guest

What's your architecture? I tried it with amd64. Also the python version prints the out while the haskell version does not, can you try it again after removing the print() statement from the python code?

comment:3 in reply to: ↑ 2 Changed 2 years ago by tibbe

Replying to guest:

What's your architecture? I tried it with amd64. Also the python version prints the out while the haskell version does not, can you try it again after removing the print() statement from the python code?

I'm using a 32-bit GHC on a 64-bit machine (Core i7.) Removing the printing from the Python code speeds it up a little bit:

$ time python pyfib.py 

real	0m12.866s
user	0m12.841s
sys	0m0.022s

comment:4 Changed 2 years ago by simonmar

  • Description modified (diff)
  • Difficulty set to Unknown

comment:5 Changed 2 years ago by simonmar

  • Resolution set to worksforme
  • Status changed from new to closed

My results on x86_64-unknown-linux with 7.4.1:

$ ghc -O2 6000.hs    
$ time ./6000        
12.76s real   12.66s user   0.10s system   99% ./6000

$ time python 6000.py
24.64s real   24.54s user   0.10s system   99% python 6000.py

Are you sure you compiled with -O2?

Note: See TracTickets for help on using tickets.