高精度计时类(C++实现)

来源:互联网 发布:苹果性能测试软件 编辑:程序博客网 时间:2024/06/06 06:10

有的时候写程序的时候需要明白自己的程序运行效率怎么样,于是就各种调用库函数,查询运行时间。但是误差在所难免,于是就有了了使用CPU周期计算时间的想法。

代码如下:

/*********************************************************************************  *CycleTimer.h;  *fmx;  *1.1;  *2014-6-15  *Description:用来提供高精度计时器功能,优化程序或者优化算法的时候提供一些参考。
  *Version:
<span style="white-space:pre"></span>1.1修正了一个错误
<span style="white-space:pre"></span>1.0实现基本功能**********************************************************************************/ class cycleTimer{public:cycleTimer();~cycleTimer();unsigned __int64 start(){m_start=GetCpuCycle();return m_start;}unsigned __int64 end(){m_end=GetCpuCycle();return m_end;}unsigned __int64 getLastTime(){m_end=GetCpuCycle();return m_end-m_start;}private:unsigned __int64 m_start;unsigned __int64 m_end; inline  unsigned __int64 GetCpuCycle(){__asm _emit 0x0F__asm _emit 0x31}};

0 0
原创粉丝点击