国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

linux 統計 程序 運行時間

2024-06-28 13:26:01
字體:
來源:轉載
供稿:網友
linux 統計 程序 運行時間

測試 代碼運行時間

linux 中的 <sys/time.h> 中 有個函數可以獲取當前時間,精確到 微秒 ----> gettimeofday()

 1 #include <sys/time.h>     // int gettimeofday(struct timeval *tv, struct timezone *tz); 2     /********************************************* 3      * struct timeval 4      * { 5      *     time_t      tv_sec;   // seconds 6      *     suseconds_t tv_usec;  // microseconds:微秒 10^(-6)s, 這里的 tv_sec 用的是 微秒 7      *                           // millisecond :毫秒 10^(-3)s 8      * } 9      **********************************************10      * struct timezone11      * {12      *     int tz_minuteswest;   // minutes west of Greenwich13      *     int tz_dsttime;       // type of DST correction14      * }15      **********************************************/

使用時,定義兩個 struct timeval 變量(通常 gettimeofday() 的第二個參數 設為 NULL),分別保存 代碼測試 前后的時刻,最后相減,即可獲取 代碼運行時間 (可轉換為自己需要的時間)。

 1 #include <stdio.h> 2 #include <sys/time.h> // for gettimeofday() 3 #include <string.h>   // for memset() 4  5 int main() 6 { 7     int i = 10000000; 8     struct timeval start, end;  // define 2 struct timeval variables 9 10     //-------------------------11     gettimeofday(&start, NULL); // get the beginning time12     //-------------------------13     14     // test code15     while(i)16     {17         i--;18     }19 20     //-------------------------21     gettimeofday(&end, NULL);  // get the end time22     //-------------------------23     24     long long total_time = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // get the run time by microsecond25     PRintf("total time is %lld us/n", total_time);26     total_time /= 1000; // get the run time by millisecond27     printf("total time is %lld ms/n", total_time);28 }測試結果:(CentOS 6.5, gcc 4.4.7)total time is 49658 ustotal time is 49 ms


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新乡县| 尼玛县| 张家口市| 浏阳市| 铜鼓县| 株洲县| 广昌县| 南乐县| 肇源县| 枝江市| 古丈县| 老河口市| 宁化县| 新河县| 达日县| 原平市| 孝义市| 湖北省| 东山县| 广昌县| 平湖市| 眉山市| 凤庆县| 睢宁县| 玉树县| 舒城县| 阆中市| 铜陵市| 柞水县| 都江堰市| 全椒县| 石首市| 兴仁县| 杂多县| 乐都县| 南昌县| 庐江县| 黄龙县| 香港 | 鹤壁市| 顺平县|