如何衡量在ATS中执行函数调用所需的时间?

时间:2018-01-09 22:45:57

标签: ats

假设我写了一个函数foo,我想找出执行foo(1000)所花费的时间。是否有可用于此的包?

1 个答案:

答案 0 :(得分:2)

如果秒数分辨率合适,您可以#include "libats/libc/DATS/time.dats"然后使用time()difftime()

implement main0() = {
  val before = time()
  val _ = foo(1000)
  val () = println!(difftime(time(), before))
}

如果你想要更好的分辨率,你可以在Linux下使用libc的gettimeofday():

extern fun reset_timer(): void = "ext#reset_timer"
extern fun elapsed_time(): double = "ext#elapsed_time"
%{
#include <sys/time.h>
#include <time.h>
struct timeval timer_timeval;
void reset_timer() { gettimeofday(&timer_timeval, NULL); }
double elapsed_time() {
  struct timeval now;
  gettimeofday(&now, NULL);
  int secs = now.tv_sec - timer_timeval.tv_sec;
  double ms = (now.tv_usec - timer_timeval.tv_usec) / ((double)1000000);
  return(secs + ms);
}
%}

/* OCaml-style helper to ignore the value of foo(1000) */
fn {a:t@ype} ignore(x: a): void = ()

implement main0() =
  begin
    reset_timer();
    ignore(foo(1000));
    println!("foo(1000) took: ", elapsed_time(), "s");
  end

输出(foo(1000)大约需要3000.3毫秒):

foo(1000) took: 3.000296s