Friday, 6 September 2013

Why is the second execution of this combinator 10 times faster than the first?

Why is the second execution of this combinator 10 times faster than the
first?

I am working on a parser combinator library and found some behavior I
couldn't explain. The first time I run the combinator it runs
significantly slower than the second time that I run it. I repo'd the
behavior with this small app (running Release with optimizations on)
let (>>=) first callback state =
let reply = first state
callback reply state
let time f =
let start = System.DateTime.Now
f()
printfn "%s" ((System.DateTime.Now - start).ToString())
[<EntryPoint>]
let main args =
let x1 state = "foo"
let compound =
x1 >>= fun i ->
x1 >>= fun j ->
x1 >>= fun a ->
x1 >>= fun b ->
x1 >>= fun c ->
x1 >>= fun d ->
x1 >>= fun e ->
x1 >>= fun f ->
x1 >>= fun j ->
fun _ -> [i;j;a;b;c;d;e;f]
time (fun () -> compound "a" |> ignore)
time (fun () -> compound "b" |> ignore)
time (fun () -> compound "c" |> ignore)
0
Running this output I get
00:00:00.0090009
00:00:00.0010001
00:00:00
Why is the first iteration so much slower than the second or third?

No comments:

Post a Comment