C++ 精确计时类

http://hi.baidu.com/ronyo/blog/item/ee7e71cf7d46c338f8dc61ad
.html
   在一些程序中经常要统计一个算法/函数花费的时间,每次都重新写代码太麻烦了,索性自己用C++写了计时类,这个类统计的时间可以精确到 微秒级别C++ <wbr>精确计时类


例子:
#include<iostream>
using namespace std;
///
#ifndef __MyTimer_H__
#define __MyTimer_H__
#include <windows.h>

class MyTimer{
private:
    int _freq;
    LARGE_INTEGER _begin;
    LARGE_INTEGER _end;
public:
    long costTime;            // 花费的时间(精确到微秒)
public:
    MyTimer(){
        LARGE_INTEGER tmp;
        QueryPerformanceFrequency(&tmp);
        _freq = tmp.QuadPart;
        costTime = 0;
    }
    void Start(){            // 开始计时
        QueryPerformanceCounter(&_begin);
    }
    void End(){                // 结束计时
        QueryPerformanceCounter(&_end);
        costTime = (long)((_end.QuadPart - _begin.QuadPart) * 1000000 / _freq);
    }
    void Reset(){            // 计时清0
        costTime = 0;
    }
};
#endif 
/
int main(){
	MyTimer mt;
	mt.Start();
	int i;
	int sum=0;
	for(i=0;i<12345678;i++){
		sum=sum+i;
	}
	mt.End();
	cout<<"Total cost time:"<<mt.costTime<< " us" << endl;
	return 0;
}





转载于:https://www.cnblogs.com/bofengyu/p/4477388.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值