如何测量在 ATS 中执行函数调用所花费的时间?
How can I measure the time taken to execute a function call in ATS?
假设我写了一个函数 foo,我想找出执行 foo(1000) 所花费的时间。是否已经有可用的软件包?
如果秒分辨率没问题,你可以#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
假设我写了一个函数 foo,我想找出执行 foo(1000) 所花费的时间。是否已经有可用的软件包?
如果秒分辨率没问题,你可以#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