精确记算程序的运行时间或者某段代码的运行时间
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
??? //精確到秒 ==========================================
??? time_t t1,t2;
??? time(&t1);
??? //此處放置要測試的代碼
??? Sleep(1000);//延時
??? time(&t2);
??? printf("%d %d %d秒\n",t1,t2,t2-t1);
??? //精確到毫秒 ========================================
??? clock_t c1,c2;
??? c1=clock();
??? //此處放置要測試的代碼
??? Sleep(100);//延時
??? c2=clock();
??? printf("%d %d %d毫秒\n",c1,c2,c2-c1);
??? //精確到 0.000001毫秒 ===============================
??? LARGE_INTEGER litmp;
??? LONGLONG start, end;
??? double dft, dff, dfm;
??? QueryPerformanceFrequency(&litmp);//獲得時鐘頻率
??? dff = (double) litmp.QuadPart;
??? QueryPerformanceCounter(&litmp);//獲得初始值
??? start = litmp.QuadPart;
??? //此處放置要測試的代碼
??? Sleep(1000);//延時
??? QueryPerformanceCounter(&litmp);//獲得終止值
??? end = litmp.QuadPart;
??? dfm = (double) (end - start);
??? dft = dfm / dff;//獲得對應的時間值,單位秒
??? printf("%lf毫秒\n",dfm/dff*1000);
}
總結
以上是生活随笔為你收集整理的精确记算程序的运行时间或者某段代码的运行时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS2010下Cannot find o
- 下一篇: Netcdf