C++實現統計代碼運行時間計時器的簡單實例
一、前言
         這里記下從網上找到的一些自己比較常用的C++計時代碼
二、Linux下精確至毫秒
#include <sys/time.h> #include <iostream> #include <time.h> double get_wall_time() {   struct timeval time ;   if (gettimeofday(&time,NULL)){     return 0;   }   return (double)time.tv_sec + (double)time.tv_usec * .000001; }  int main() {   unsigned int t = 0;   double start_time = get_wall_time()   while(t++<10e+6);   double end_time = get_wall_time()   std::cout<<"循環耗時為:"<<end_time-start_time<<"ms";   return 0; } 三、Windows下精確至毫秒
#include <windows.h> #include <iostream>  int main() {   DWORD start, stop;   unsigned int t = 0;   start = GetTickCount();   while (t++ < 10e+6);   stop = GetTickCount();   printf("time: %lld ms/n", stop - start);   return 0; } 試驗中,發現貌似getTickCount函數會有10幾毫秒的誤差, 主站蜘蛛池模板: 焉耆| 绥中县| 孝昌县| 夏邑县| 镇江市| 曲阜市| 乌兰县| 南涧| 伊宁县| 巩义市| 尖扎县| 红桥区| 尼玛县| 岳池县| 紫云| 宝应县| 吉首市| 宁波市| 浦城县| 都江堰市| 德安县| 基隆市| 扶余县| 鹰潭市| 抚远县| 建宁县| 岑巩县| 东方市| 莆田市| 平利县| 西充县| 上饶县| 云霄县| 咸阳市| 肇州县| 南部县| 当阳市| 远安县| 洛宁县| 永康市| 河间市|