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
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.