自己封转了一个windows下计时的函数:
double time_cost()
{
LARGE_INTEGER m_nFreq;
LARGE_INTEGER CurrentTime;
//LARGE_INTEGER nEndTime;
QueryPerformanceFrequency(&m_nFreq); // 获取时钟周期
QueryPerformanceCounter(&CurrentTime); // 获取时钟计数
return (double)CurrentTime.QuadPart/m_nFreq.QuadPart;
}
测试函数:
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
double time_cost()
{
LARGE_INTEGER m_nFreq;
LARGE_INTEGER CurrentTime;
//LARGE_INTEGER nEndTime;
QueryPerformanceFrequency(&m_nFreq); // 获取时钟周期
QueryPerformanceCounter(&CurrentTime); // 获取时钟计数
return (double)CurrentTime.QuadPart/m_nFreq.QuadPart;
}
int main()
{
double t1 = time_cost();
Sleep(1000); //1 second
double t2 = time_cost();
cout<<"Cost time: "<< t2-t1<<endl;
}
收集了Windows && Linux 下能实现较高精度的定时器实现。
Windows的高精度计时:
(转自http://topic.youkuaiyun.com/t/20030617/08/1923620.html)
对于高计时精度,要采用QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数。QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数的原型为:
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpCount) ;
数据类型LARGE—INTEGER既可以是一个作为8字节长的整型数,也可以是作为两个4字节长的整型数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下:
typedef union _LARGE_INTEGER {
struct{
DWORD LowPart; // 4字节整型数
LONG HighPart; // 4字节整型数
};
LONGLONG QuadPart;// 8字节整型数
}LARGE_INTEGER;
计时之前,先调用QueryPerformanceFrequency()函数获得机器内部计时器的时钟频率。接着,在需要严格计时的事件发生之前和发生之后分别调用QueryPerformanceCounter()函数,利用两次获得的计数之差和时钟频率,算出事件经历的精确时间。
LARGE_INTEGER litmp;
LONGLONG QStart,QEnd;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp); //获得计数器的时钟频率
dfFreq = (double)litmp.QuadPart;
QueryPerformanceCounter(&litmp); //获得初始值
QStart = litmp.QuadPart;
//你的测试代码
QueryPerformanceCounter(&litmp); //获得终止值
QEnd = litmp.QuadPart;
dfMinus = (double)(QStart - QEnd);
dfTim = dfMinus / dfFreq; //获得对应的时间值
//dfTim 为得到的结果。
Linux下的高精度计时:
转自http://www.cnblogs.com/daqiwancheng/archive/2010/07/01/1769522.html
Linux下使用clock_gettime给程序计时
哦,clock_gettime( ) 提供了纳秒的精确度,给程序计时可是不错哦;函数的原型如下:
int clock_gettime(clockid_t clk_id, struct timespect *tp);
clockid_tclk_id用于指定计时时钟的类型,对于我们Programmr以下三种比较常用:
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
CLOCK_REALTIME, a system-wide realtime clock.CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
struct
timespect *tp用来存储当前的时间,其结构如下:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
呵呵,好啦!该讲的都刚清楚了,下面我们就上代码吧;代码
#include <iostream>
#include <time.h>
using namespace std;
timespec diff(timespec start, timespec end);
int main()
{
timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i< 242000000; i++)
temp+=temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
return 0;
}
timespec diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}