C++ 记录程序运行时间间隔

方式一

#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    clock_t startTime,endTime;
    // 计时开始
    startTime = clock();
    for (int i = 0; i < 2147483640; i++)
    {
        i++;
    }
    // 计时结束
    endTime = clock();
    cout << "运行时间: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "秒" << endl;
    system("pause");
    return 0;
}
// 注释在:VC++6.0中可以用CLK_TCK替换CLOCKS_PER_SEC。

方式二

clock_t startTime, endTime;
// 计时开始
startTime = clock();
// 计时结束
endTime = clock();
CString strLog;
strLog.Format(_T("运行时间: %0.3lf秒\\r\\n"), (double)(endTime - startTime) / CLOCKS_PER_SEC);
OutputDebugString(strLog);

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注