C++中获取高精度时间差

本文介绍在Windows和Linux环境下实现高精度计时的方法。Windows部分通过QueryPerformanceFrequency和QueryPerformanceCounter函数来获取计时器频率和计数值,进而计算时间间隔。Linux部分则利用clock_gettime函数和timespec结构体进行高精度计时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己封转了一个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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值