Project Euler 14: performance compared to C and memoization

后端 未结 3 572
情歌与酒
情歌与酒 2021-01-11 14:34

I\'m currently working on project euler problem 14.

I solved it using a poorly coded program, without memoization, that took 386 5 seconds to run (s

3条回答
  •  -上瘾入骨i
    2021-01-11 14:41

    Well, the C program uses unsigned long, but Integer can store arbitrarily large integers (it's a bignum). If you import Data.Word, then you can use Word, which is a machine-word-sized unsigned integer.

    After replacing Integer with Word, and using ghc -O2 and gcc -O3, the C program runs in 0.72 seconds, while the Haskell programs runs in 1.92 seconds. 2.6x isn't bad. However, ghc -O2 doesn't always help, and this is one of the programs on which it doesn't! Using just -O, as you did, brings the runtime down to 1.90 seconds.

    I tried replacing div with quot (which uses the same type of division as C; they only differ on negative inputs), but strangely it actually made the Haskell program run slightly slower for me.

    You should be able to speed up the syr function with the help of this previous Stack Overflow question I answered about the same Project Euler problem.

提交回复
热议问题