import qualified Data.MemoCombinators as Memo
import System.Environment
single = fib' 80000
where fib' = Memo.integral fib''
fib'' 0 = 0
fib'' 1 = 1
fib'' x = fib' (x-1) + fib' (x-2)
doubleFast = fib' 80000 + fib' 80000
where fib' = Memo.integral fib''
fib'' 0 = 0
fib'' 1 = 1
fib'' x = fib' (x-1) + fib' (x-2)
doubleSlow = g 80000 + g 80000
where g x = fib' x
where fib' = Memo.integral fib''
fib'' 0 = 0
fib'' 1 = 1
fib'' x = fib' (x-1) + fib' (x-2)
main = do
args <- getArgs
case args of
["single"] -> print single
["doubleFast"] -> print doubleFast
["doubleSlow"] -> print doubleSlow
./文件单 +RTS -s
产量
1,085,072,320 bytes allocated in the heap
387,297,448 bytes copied during GC
265,811,512 bytes maximum residency (10 sample(s))
99,107,440 bytes maximum slop
433 MB total memory in use (0 MB lost due to fragmentation)
Total time 2.78s ( 2.78s elapsed)
./file doubleFast +RTS -s
产生相同的结果。这对我来说很有意义:由于fib'
在 的范围内doubleFast
,fib'
计算第一个后不会丢弃fib' 80000
,可以用来直接给出第二个fib' 80000
。
我不明白的是:
./file doubleSlow +RTS -s
2,166,532,752 bytes allocated in the heap
551,826,896 bytes copied during GC
263,793,848 bytes maximum residency (11 sample(s))
204,460,968 bytes maximum slop
818 MB total memory in use (0 MB lost due to fragmentation)
Total time 14.22s ( 14.24s elapsed)
如果我错了,请纠正我:fib'
用于计算第一个g 80000 = fib' 80000
. 剩下的函数g
,因为g没有被记忆,所以不能重用g 80000
直接计算秒。此外,在离开g 80000
查找表的 第一次调用后,fib'
由于不在 的范围内,它被丢弃doubleSlow
。
800 MB 对我来说很有意义,因为查找表必须构建两次。但是,为什么比 2.78s 需要 14.22s?我预计大约是两倍,大约。5.8 秒。