Why does this time function always measure 0 ms?

I define some algorithms and come up with a time function below. However, it always returns 0 ms.

The questions are why it is always 0ms when it obviously takes a few seconds. I'm starting an F # developer, so I probably miss some concepts.

Please note that the question is not about a more efficient Fibonacci algorithm, and I also know that the function measures real world time, not processor time (which can be obtained by Sys.time ())

let time f x =
   let timer = new System.Diagnostics.Stopwatch()
   timer. Start ( )
   try f x finally
   printf "Took %dms" timer.ElapsedMilliseconds;;

let rec fib x = 
   if x < 2 then 1
   else fib(x-1) + fib(x-2)

time Array.iter (fun x -> ignore (fib x) ) [| 1 .. 40 |] 

Thanks for any help and pointers for the novice F # developer

Regards, Tom

+5
source share
2 answers

, , :

time Array.iter (fun x -> ...) [|1..40|]
                 ^- first arg   ^- second arg

,

time (Array.iter (fun x -> ignore (fib x) )) [| 1 .. 40 |] 
      ^- a single partially curried function  ^- a single argument

, FSI:

> time ( Array.iter (fun x -> ignore (fib x) ) ) [| 1 .. 40 |];;
Took 6589msval it : unit = ()

, F # interactive, #time, FSI . :

> #time;;

--> Timing now on

> Array.iter (fun x -> ignore (fib x) ) [| 1 .. 40 |];;
Real: 00:00:06.816, CPU: 00:00:06.218, GC gen0: 0, gen1: 0, gen2: 0
val it : unit = ()
+6

, - , -, :

((time Array.iter) (fun x -> ignore (fib x))) [| 1 .. 40 |]

, , Array.iter fun x -> ignore (fib x), int array -> (), [| 1 .. 40 |].

time (Array.iter (fun x -> ignore (fib x))) [| 1 .. 40 |] 
+5

All Articles