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;
    }
  • 相关阅读:
    Windows安装Oracle
    MySQL索引的创建和使用
    使用TensorFlow进行中文情感分析
    gensim库word2vec使用
    软件测试理论
    产品经理
    产品经理
    产品经理
    职业规划及核心竞争力
    项目管理
  • 原文地址:https://www.cnblogs.com/bl4nk/p/2022992.html
Copyright © 2011-2022 走看看