作者Avogadro (盖立安800)
看板C_and_CPP
标题Re: [问题]请问Linux下的计时器
时间Wed Apr 12 21:13:58 2006
谢谢回应
我写了一个小副程
有需要的可以拿去用
========================================================================
#include <sys/time.h>
timeval tv_Start,tv_End;
double StopWatch(bool b_Stop)
{
//usage : StopWatch(0) -> Set up the stopwatch
// StopWatch(1) -> Stop the stopwatch
//return : StopWatch(0) return 0.0;
// StopWatch(1) return time interval in sec.
if(!b_Stop)
{
gettimeofday(&tv_Start,NULL);
return 0.0;
}
else
{
gettimeofday(&tv_End,NULL);
return (1000000*(tv_End.tv_sec-tv_Start.tv_sec)+
tv_End.tv_usec-tv_Start.tv_usec)/1000000.0;
}
}
========================================================================
※ 引述《littleshan (我要加入剑道社!)》之铭言:
: 这个方法我也常用
: 不过我会建议 gettimeofday
: 最主要是因为 rdtsc 需要先知道 cpu clock 才能转成 ms
: 另外就是如果 cpu 有省电功能
: 通常 clock 是不固定的
: 所以结果会有误差
: 另外在 smp 系统上
: 两颗 cpu 的 timestamp 可能不同步
: (还有 你竟然直接写machine code 真帅气 XD)
: ※ 引述《ccbruce (万年好人)》之铭言:
: : try RDTSC on Intel family...
: : The resolution is in n-sec level.
: : __inline__ unsigned long long int rdtsc()
: : {
: : unsigned long long int x;
: : __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
: : return x;
: : }
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.115.155.35