Ticket #6000 (closed bug: worksforme)

Opened 13 months ago

Last modified 13 months ago

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

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

fibs.hs Download (204 bytes) - added by guest 13 months ago.
Haskell implementation
pyfib.py Download (191 bytes) - added by guest 13 months ago.
Python implementation

Change History

Changed 13 months ago by guest

Haskell implementation

Changed 13 months ago by guest

Python implementation

  Changed 13 months 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

follow-up: ↓ 3   Changed 13 months 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?

in reply to: ↑ 2   Changed 13 months 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

  Changed 13 months ago by simonmar

  • difficulty set to Unknown
  • description modified (diff)

  Changed 13 months ago by simonmar

  • status changed from new to closed
  • resolution set to worksforme

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.