Haskell基准测试/非严格约简的nf/whnf优化
我正在尝试优化一个旨在获取大型数据集的库 然后对其应用不同的操作。现在图书馆正在工作,我想要 来优化它。
我的印象是非严格的评估允许 GHC 组合操作,以便当所有数据都只迭代一次 函数的编写方式是对参数进行排序,以便于减少 whnf。 (并且为了潜在地减少对每个数据执行的操作数量)
为了测试这一点,我编写了以下代码:
import Criterion.Main
main = defaultMain
[ bench "warmup (whnf)" $ whnf putStrLn "HelloWorld",
bench "single (whnf)" $ whnf single [1..10000000],
bench "single (nf)" $ nf single [1..10000000],
bench "double (whnf)" $ whnf double [1..10000000],
bench "double (nf)" $ nf double [1..10000000]]
single :: [Int] -> [Int]
single lst = fmap (* 2) lst
double :: [Int] -> [Int]
double lst = fmap (* 3) $ fmap (* 2) lst
使用 Criterion 库进行基准测试我得到以下结果:
benchmarking warmup (whnf)
mean: 13.72408 ns, lb 13.63687 ns, ub 13.81438 ns, ci 0.950
std dev: 455.7039 ps, lb 409.6489 ps, ub 510.8538 ps, ci 0.950
benchmarking single (whnf)
mean: 15.88809 ns, lb 15.79157 ns, ub 15.99774 ns, ci 0.950
std dev: 527.8374 ps, lb 458.6027 ps, ub 644.3497 ps, ci 0.950
benchmarking single (nf)
collecting 100 samples, 1 iterations each, in estimated 107.0255 s
mean: 195.4457 ms, lb 195.0313 ms, ub 195.9297 ms, ci 0.950
std dev: 2.299726 ms, lb 2.006414 ms, ub 2.681129 ms, ci 0.950
benchmarking double (whnf)
mean: 15.24267 ns, lb 15.17950 ns, ub 15.33299 ns, ci 0.950
std dev: 384.3045 ps, lb 288.1722 ps, ub 507.9676 ps, ci 0.950
benchmarking double (nf)
collecting 100 samples, 1 iterations each, in estimated 20.56069 s
mean: 205.3217 ms, lb 204.9625 ms, ub 205.8897 ms, ci 0.950
std dev: 2.256761 ms, lb 1.590083 ms, ub 3.324734 ms, ci 0.950
GHC 是否优化了“double”函数,以便列表仅 由 (* 6) 操作过一次? nf 结果表明情况确实如此,因为 否则“double”的平均计算时间将是“single”的两倍。whnf
版本运行如此快的区别是什么?我可以 只假设实际上没有执行任何操作(或者只是第一个 减少迭代)
我是否使用了正确的术语?
I am trying to optimize a library which is designed to take a large data set and
then apply different operations to it. Now that the library is working, I want
to optimize it.
I am under the impression that non-strict evaluation allows
GHC to combine operations so that the data is only iterated over once when all
of the functions are written so that arguments are ordered to facilitate whnf reduction.
(And to potentially reduce the number of operations performed on each datum)
To test this I wrote the following code:
import Criterion.Main
main = defaultMain
[ bench "warmup (whnf)" $ whnf putStrLn "HelloWorld",
bench "single (whnf)" $ whnf single [1..10000000],
bench "single (nf)" $ nf single [1..10000000],
bench "double (whnf)" $ whnf double [1..10000000],
bench "double (nf)" $ nf double [1..10000000]]
single :: [Int] -> [Int]
single lst = fmap (* 2) lst
double :: [Int] -> [Int]
double lst = fmap (* 3) $ fmap (* 2) lst
Benchmarking using the Criterion library I get the following results:
benchmarking warmup (whnf)
mean: 13.72408 ns, lb 13.63687 ns, ub 13.81438 ns, ci 0.950
std dev: 455.7039 ps, lb 409.6489 ps, ub 510.8538 ps, ci 0.950
benchmarking single (whnf)
mean: 15.88809 ns, lb 15.79157 ns, ub 15.99774 ns, ci 0.950
std dev: 527.8374 ps, lb 458.6027 ps, ub 644.3497 ps, ci 0.950
benchmarking single (nf)
collecting 100 samples, 1 iterations each, in estimated 107.0255 s
mean: 195.4457 ms, lb 195.0313 ms, ub 195.9297 ms, ci 0.950
std dev: 2.299726 ms, lb 2.006414 ms, ub 2.681129 ms, ci 0.950
benchmarking double (whnf)
mean: 15.24267 ns, lb 15.17950 ns, ub 15.33299 ns, ci 0.950
std dev: 384.3045 ps, lb 288.1722 ps, ub 507.9676 ps, ci 0.950
benchmarking double (nf)
collecting 100 samples, 1 iterations each, in estimated 20.56069 s
mean: 205.3217 ms, lb 204.9625 ms, ub 205.8897 ms, ci 0.950
std dev: 2.256761 ms, lb 1.590083 ms, ub 3.324734 ms, ci 0.950
Does GHC optimize the "double" function so that the list is only
operated on once by (* 6)? The nf results show that this is the case because
otherwise the mean computation time for "double" would be twice that of "single"
What is the difference that makes the whnf version run so fast? I can
only assume that nothing is actually being performed (OR just the first
iteration in the reduction)
Am I even using the correct terminology?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 GHC 使用
-ddump-simpl
选项生成的核心(中间代码),我们可以确认 GHC 确实将map
的两个应用程序融合为一个(使用-O2
)。转储的相关部分是:请注意,
Main.double
中仅使用了一次GHC.Base.map
,指的是组合函数Main。 main10
既加 2 又乘以 3。这可能是 GHC 首先内联列表的Functor
实例的结果,以便fmap
变成map
,然后 应用重写规则,允许两个应用程序 < code>map 进行融合,加上一些更多的内联和其他优化。WHNF 意味着表达式仅计算为“最外层”数据构造函数或 lambda。在本例中,这意味着第一个
(:)
构造函数。这就是为什么速度如此之快,因为几乎没有做任何工作。请参阅我对什么是弱头范式? 了解更多详情。Looking at the core (intermediate code) generated by GHC using the
-ddump-simpl
option, we can confirm that GHC does indeed fuse the two applications ofmap
into one (using-O2
). The relevant parts of the dump are:Note how there is only one use of
GHC.Base.map
inMain.double
, referring to the combined functionMain.main10
which both adds 2 and multiplies by 3. This is likely a result of GHC first inlining theFunctor
instance for lists so thatfmap
becomesmap
, and then applying a rewrite rule that allows two applications ofmap
to be fused, plus some more inlining and other optimizations.WHNF means that the expression is only evaluated to the "outermost" data constructor or lambda. In this case, that means the first
(:)
constructor. That's why it's so much faster, since almost no work is being done. See my answer to What is Weak Head Normal Form? for more details.