Preventing caching of computation in Criterion benchmark

前端 未结 1 1248
时光取名叫无心
时光取名叫无心 2021-01-19 12:39

The following code (suggested by Reid Barton at Criterion causing memory consumption to explode, no CAFs in sight) has a benchmark time which scales proportional

1条回答
  •  情歌与酒
    2021-01-19 13:20

    The result is being cached in your lvl_r6yu. You can see that lst1 is [0..num] lifted out to the top level, and from $wlgo 0 lst1 it can be seen that the result of the summation is lifted out too.

    It's easier to see what's happening if we add the top level definition foo = mysum . lst, and then look at the core for foo. You can see there that foo is a constant function returning the result of the summation.

    If we add {-# OPTIONS -fno-full-laziness #-}, then subexpressions will not be lifted, and therefore the benchmark will work as intended.

    It is a good idea in general when using criterion to control evaluation through the arguments supplied to whnf. In our case:

    bench "mysum" $ whnf (\size -> mysum [1..size]) num
    

    This works fine regardless of optimization or lifting.

    0 讨论(0)
提交回复
热议问题