Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 如何正确计时ocaml程序?_Unix_Ocaml - Fatal编程技术网

Unix 如何正确计时ocaml程序?

Unix 如何正确计时ocaml程序?,unix,ocaml,Unix,Ocaml,我使用命令 let t = Unix.gettimeofday() let rec nothing i = match i with | 1000000000 -> 1 | _ -> nothing (i+1) let () = Printf.printf "%d %fs\n" (nothing 0) (Unix.gettimeofday() -. t) 把它编译成字节码。执行时间显然是几秒钟,但程序打印0.000001秒 如果我用Sys.time()替换Unix.ge

我使用命令

let t = Unix.gettimeofday()
let rec nothing i =
  match i with
  | 1000000000 -> 1
  | _ -> nothing (i+1)
let () = Printf.printf "%d %fs\n" (nothing 0) (Unix.gettimeofday() -. t)
把它编译成字节码。执行时间显然是几秒钟,但程序打印0.000001秒

如果我用Sys.time()替换Unix.gettimeofday(),则它仅为0.000000s

utop
中运行代码也不是更好。它给出了大约0.02秒,而我至少计算了5秒


我想知道这里出了什么问题?我使用的是Debian Wheezy,ocaml版本是4.02.1。

printf函数以及任何其他函数的参数都是按未指定的顺序计算的。在您的例子中,
(Unix.gettimeofday()-.t)
表达式是在
(nothing 0)
之前计算的。更合适的代码版本是:

ocamlc unix.cma test.ml

您确定在
nothing 0
之前计算了
Unix.gettimeofday()
?我不知道OCaml,但它看起来一点也不像。为什么不呢?正如@ivg所说,函数参数的求值顺序是未指定的。我明白了,渴望是关于在电话前是否对他们进行了评估,而不是顺序。我认为前面的海报是指
let t=Unix.gettimeofday()
。关于执行计时,还可以看看
val Unix.times:unit->process\u times
,它提供了Unix时间实用程序之类的数据。
let rec nothing i =
  match i with
  | 1000000000 -> 1
  | _ -> nothing (i+1)


let () =
  let t = Unix.gettimeofday() in 
  Printf.printf "%ds\n" (nothing 0);
  Printf.printf "time elapsed: %g s\n" (Unix.gettimeofday() -. t)