定义并实现统计代码执行时间的类

类别:.NET开发 点击:0 评论:0 推荐:

/// <summary>
/// Copyright (c) 2002, AllAboutProgram BBS.
/// All rights reserved.
///
/// 文件:PerformTime.h
/// 摘要:定义并实现统计代码执行时间的类
/// </summary>
///
/// <remark>
/// 当前版本:1.0
/// 作    者:小刀
/// 完成日期:2002年9月18日
/// </remark>

#ifndef PERFORMTIME_H
#define PERFORMTIME_H

#include <windows.h>
#include <string>
#include <sstream>

/// <summary> 代码执行时间类 </summary>
class CPerformTime
{
public:
    /// <summary> 缺省构造函数 </summary>
    CPerformTime()
    {
        m_llStartCount    = 0;
        m_llEndCount    = 0;
        m_ldDiffSeconds    = 0;

        // 高精度运行计数器的频率
        m_bSupported = QueryPerformanceFrequency((PLARGE_INTEGER)&m_llFrequency);
        if (!m_bSupported)
        {
            // 不支持高精度运行计数器
            std::cout << "QueryPerformance functions are not supported!\n\n";
        }
    }

    /// <summary> 开始统计 </summary>
    /// <returns> 开始统计时高精度运行计数器的数值 </returns>
    LONGLONG Start()
    {
        m_ldDiffSeconds = 0;
        if (m_bSupported)
        {
            // 记录高精度运行计数器的数值
            QueryPerformanceCounter((PLARGE_INTEGER)&m_llStartCount);
        }
        else
        {
            // 不支持高精度运行计数器
            std::cout << "QueryPerformance functions are not supported!\n\n";
        }
        return m_llStartCount;
    }

    /// <summary> 结束统计 </summary>
    /// <returns> 结束统计时高精度运行计数器的数值 </returns>
    LONGLONG Finish()
    {
        long lHours;
        long lMinutes;
        long double ldSeconds;
        std::ostringstream formatstr;

        if (m_bSupported)
        {
            // 记录高精度运行计数器的数值
            QueryPerformanceCounter((PLARGE_INTEGER)&m_llEndCount);

            // 计算以秒为单位的运行时间
            m_ldDiffSeconds = (long double)(m_llEndCount - m_llStartCount) / (long double)m_llFrequency;

            // 分离运行时间的时分秒
            lHours = m_ldDiffSeconds / 3600;
            lMinutes = (m_ldDiffSeconds - lHours * 3600) / 60;
            ldSeconds = m_ldDiffSeconds - lHours * 3600 - lMinutes * 60;

            // 格式化运行时间字符串
            formatstr << lHours << " hours " << lMinutes << " minutes and " << ldSeconds << " seconds";
            m_strDiffTime = formatstr.str();
        }
        else
        {
            // 不支持高精度运行计数器
            std::cout << "QueryPerformance functions are not supported!\n\n";
        }
        return m_llEndCount;
    }

    /// <summary> 获得高精度运行计数器的频率 </summary>
    /// <returns> 高精度运行计数器的频率(单位:n/s) </returns>
    LONGLONG Frequency() const
    {
        return m_llFrequency;
    }

    /// <summary> 获得开始统计时高精度运行计数器的数值 </summary>
    /// <returns> 开始统计时高精度运行计数器的数值 </returns>
    LONGLONG StartCount() const
    {
        return m_llStartCount;
    }

    /// <summary> 获得结束统计时高精度运行计数器的数值 </summary>
    /// <returns> 结束统计时高精度运行计数器的数值 </returns>
    LONGLONG EndCount() const
    {
        return m_llEndCount;
    }

    /// <summary> 获得代码运行时间 </summary>
    /// <returns> 代码运行时间(单位:s) </returns>
    long double DiffSeconds() const
    {
        return m_ldDiffSeconds;
    }

    /// <summary> 获得代码运行时间 </summary>
    /// <returns> 代码运行时间(字符串格式) </returns>
    const std::string& DiffTime() const
    {
        return m_strDiffTime;
    }

private:
    BOOL        m_bSupported;        // 是否支持高精度运行计数器
    LONGLONG    m_llFrequency;        // 高精度运行计数器的频率(单位:n/s)
    LONGLONG    m_llStartCount;        // 开始统计时高精度运行计数器的数值
    LONGLONG    m_llEndCount;        // 结束统计时高精度运行计数器的数值
    long double    m_ldDiffSeconds;    // 代码运行时间(单位:s)
    std::string    m_strDiffTime;        // 字符串格式的代码运行时间
};

#endif //PERFORMTIME_H

 

示例:

Code:
int main(int argc, char* argv[])
{
    CPerformTime pt;
    pt.Start();
    long ert = 10000;
    while (ert--)
        ;
    pt.Finish();
    cout << "The frequency of the high-resolution performance counter is " << (long)pt.Frequency() << "/s.\n";
    cout << "The diff-count is " << (long)(pt.EndCount() - pt.StartCount()) << ".\n";
    cout << "The performance experienced " << pt.DiffTime() << "(total " << pt.DiffSeconds() << "s).\n\n";
    return 0;
}

 

输出:
The frequency of the high-resolution performance counter is 3579545/s.
The diff-count is 434.
The performance experienced 0 hours 0 minutes and 0.000121244 seconds(total 0.000121244s).

精度:
约1/3微秒(3.33e-7秒)甚至更高。

本文地址:http://com.8s8s.com/it/it42075.htm