Why is the creation of Lazy type so slow?
Assume the following code:
type T() =
let v = lazy (0.0)
member o.a = v.Value
type T2() =
member o.a = 0.0
#time "on"
for i in 0 .. 10000000 do
T() |> ignore
#time "on"
for i in 0 .. 10000000 do
T2() |> ignore
The first loop gives me: Real: 00:00:00.647 whereas the second loop gives me Real: 00:00:00.051. Lazy is 13X slower!!
I have tried to optimize my code in this way and I ended up with simulation code 6X slower. It was then fun to track back where the slow down occurred…
The Lazy version has some significant overhead code –
Compare this to the direct version
So the direct version has a load and then return, whilst the indirect version has a load then a call and then a return.
Since the
lazyversion has an extra call I would expect it to be significantly slower.UPDATE:
So I wondered if we could create a custom version of
lazywhich did not require the method calls – I also updated the test to actual call the method rather than just create the objects. Here is the code:Which gives the following result:
Here the
lazyversion is only 2x slower than the direct version and the fake lazy version is even slightly faster than the direct version – this is probably due to a GC happening during the benchmark.