Lecture chapter 12 by Erik通过引入示例函数来提及“提高空间使用率”sumWith
.
我代表惰性版本和严格版本如下。
sumWith1 v [] = v
sumWith1 v (x:xs) = sumWith1 (v+x) xs
sumWith2 v [] = v
sumWith2 v (x:xs) = (sumWith2 $! (v+x)) xs
test = sumWith1 0 [1..200000000]
我猜严格版本应该在一定程度上提高性能
我尝试验证利用 GHC 分析工具。
$ ghc --make -O2 -prof -auto-all -rtsopts -o test1
$ ./test1 +RTS -p -RTS
更改
sumWith1
至 sumWith2
在 test
函数并再次作为 test2 执行。这是分析结果:http://pastie.org/4720019 .
通过查看
%alloc
列,我看不出这两个函数之间有任何区别。我的问题是我应该如何改进测试用例以找到一些差异。
换句话说,是否可以分析这种情况下的空间使用情况?
谢谢你。
最佳答案
使用 GHC 的堆分析器。
在RWH, ch25中有详细描述.这是a fully worked example .
关于haskell - 是否可以在 Haskell 中分析空间使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426266/