zoukankan      html  css  js  c++  java
  • C++ 精确计时类

      因为平时经常要测试代码的执行效率,所以非常需要一个Timer.

      So,上网搜了下还真有 http://blog.sina.com.cn/s/blog_5fe506110100dgfd.html

      在此感谢作者,自己修改了下,收藏。

    //精确计时类
    #ifndef __MyTimer_H__
    #define __MyTimer_H__
    #include
    <windows.h>

    class MyTimer
    {
    private:
    LARGE_INTEGER _freq;
    LARGE_INTEGER _begin;
    LARGE_INTEGER _end;

    public:
    double costTime; // 花费的时间 , 根据自己需要换算时间单位

    public:
    MyTimer()
    {
    QueryPerformanceFrequency(
    &_freq);//获得计数器(cpu)的时钟频率
    costTime = 0;
    }

    void Start() // 开始计时
    {
    QueryPerformanceCounter(
    &_begin);
    }

    void Stop() // 结束计时
    {
    QueryPerformanceCounter(
    &_end);
    costTime
    = (double)((_end.QuadPart - _begin.QuadPart) * 1000000 / _freq.QuadPart); //花费的时间(精确到微秒 us )
    }

    void Reset() // 计时清0
    {
    costTime
    = 0;
    }
    };
    #endif
    int main()
    {
    vector
    <int> num;
    int i,element;
    int array[C_Size];
    MyTimer mt;

    cout
    << "排序规模为:" << C_Size << endl;

    for(i=0;i<C_Size;i++)
    {
    element
    = rand()%1000;
    num.push_back(element);
    array[i]
    = rand()%1000;
    }

    mt.Start();
    sort(num.begin(),num.end());
    mt.End();
    cout
    << "sort() cost time:" << mt.costTime << " us" << endl;
    }
  • 相关阅读:
    css3 过渡
    2021.1.5 算法实训
    表单 form
    表格 table
    Windows系统重装记录
    多线程【基础】
    关于excuteQuery与execute()
    关于jsp的action如何调用servlet的自定义方法
    selenium
    验证码处理
  • 原文地址:https://www.cnblogs.com/bl4nk/p/2022992.html
Copyright © 2011-2022 走看看