Anonymous user
Time a function: Difference between revisions
added ocaml
(add Common Lisp example) |
(added ocaml) |
||
Line 44:
=={{header|C++}}==
}</cpp>
===Example===
Line 108:
(6!:2,7!:2) '|: 50 50 50 $ i. 50^3'
0.00387912 1.57414e6
=={{header|OCaml}}==
<ocaml>let time_it action arg =
let start_time = Sys.time () in
ignore (action arg);
let finish_time = Sys.time () in
finish_time -. start_time</ocaml>
===Example===
# Printf.printf "Identity(4) takes %f seconds.\n" (time_it (fun x -> x) 4);;
Identity(4) takes 0.000000 seconds.
- : unit = ()
# let sum x = let num = ref x in for i = 0 to 999999 do num := !num + i done; !num;;
val sum : int -> int = <fun>
# Printf.printf "Sum(4) takes %f seconds.\n" (time_it sum 4);;
Sum(4) takes 0.084005 seconds.
- : unit = ()
=={{header|Python}}==
Line 113 ⟶ 130:
'''Note:''' There is an overhead in executing a function that does nothing.
def nothing(): pass
def identity(x): return x</python>
===Example===
>>> print usec(nothing, [])
|