【发布时间】:2015-01-16 20:00:44
【问题描述】:
我想问你们这里的任何人都熟悉这个功能,如下面的Interbench。我想将此移植到 Windows 平台,但一直失败。我只能通过使用 timeval 而不是 timespec 来获得微秒精度。最后会出现错误:除以零和访问冲突异常
unsigned long get_usecs(struct timeval *myts)
{
if (clock_gettime(myts))
terminal_error("clock_gettime");
return (myts->tv_sec * 1000000 + myts->tv_usec);
}
void burn_loops(unsigned long loops)
{
unsigned long i;
/*
* We need some magic here to prevent the compiler from optimising
* this loop away. Otherwise trying to emulate a fixed cpu load
* with this loop will not work.
*/
for (i = 0; i < loops; i++)
_ReadWriteBarrier();
}
void calibrate_loop()
{
unsigned long long start_time, loops_per_msec, run_time = 0;
unsigned long loops;
struct timeval myts;
loops_per_msec = 100000;
redo:
/* Calibrate to within 1% accuracy */
while (run_time > 1010000 || run_time < 990000) {
loops = loops_per_msec;
start_time = get_usecs(&myts);
burn_loops(loops);
run_time = get_usecs(&myts) - start_time;
loops_per_msec = (1000000 * loops_per_msec / run_time ? run_time : loops_per_msec );
}
/* Rechecking after a pause increases reproducibility */
Sleep(1 * 1000);
loops = loops_per_msec;
start_time = get_usecs(&myts);
burn_loops(loops);
run_time = get_usecs(&myts) - start_time;
/* Tolerate 5% difference on checking */
if (run_time > 1050000 || run_time < 950000)
goto redo;
loops_per_ms = loops_per_msec;
}
【问题讨论】:
-
您提供的函数很大程度上依赖于函数
get_usecs()来计算时间,并且它还依赖函数burn_loops()和Sleep()来实现其某些行为。 这些都不是标准的 C 甚至 POSIX 函数,所以你没有给我们足够的信息来提供适当的答案。 -
@JohnBollinger,我已经添加了所需的功能。
标签: c linux windows benchmarking