GHC Optimization: Collatz conjecture

后端 未结 2 1853
夕颜
夕颜 2021-01-11 13:32

I\'ve written code for the Project Euler\'s Challenge 14, in both Haskell and C++ (ideone links). They both remember any calculations they have previously done in an array.

2条回答
  •  抹茶落季
    2021-01-11 14:15

    The ideone site is using a ghc 6.8.2, which is getting pretty old. On ghc version 7.4.1, the difference is much smaller.

    With ghc:

    $ ghc -O2 euler14.hs && time ./euler14
    (837799,329)
    ./euler14  0.63s user 0.04s system 98% cpu 0.685 total
    

    With g++ 4.7.0:

    $ g++ --std=c++0x -O3 euler14.cpp && time ./a.out
    8400511 429
    ./a.out  0.24s user 0.01s system 99% cpu 0.252 total
    

    For me, the ghc version is only 2.7 times slower than the c++ version. Also, the two programs aren't giving the same result... (not a good sign, especially for benchmarking)

提交回复
热议问题