https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c 關鍵結構體: 程式: 保持更新,如果對您有幫助請點擊推薦!更多關於Linux 相關的知識,請關註 cnblogs.com/xuyaowen ...
https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c
關鍵結構體:
struct rusage { struct timeval ru_utime; /* user time used */ struct timeval ru_stime; /* system time used */ long ru_maxrss; /* max resident set size */ long ru_ixrss; /* integral shared text memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims */ long ru_majflt; /* page faults */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* messages sent */ long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ };
程式:
/** * Author: Yaowen Xu * Github: https://github.com/yaowenxu * Organization: 北航系統結構研究所 * Date: 2019-08-18 13:31:34 * LastEditTime: 2019-08-18 13:51:26 * Description: 使用系統 函數 getrusage 獲取程式運行相關信息 * 此程式主要是關註與總時間和內核空間運行時間與用戶 * 空間運行時間,使用此函數可大致對程式運行時間計算; * 查看: 具體使用信息可以在控制台以 man getrusage 命令查看 */ #include <stdio.h> #include <math.h> #include <sys/time.h> #include <sys/resource.h> int str2int(char* str){ char *p = str; int sum = 0; while (*p != '\0') { sum = sum*10 + (*p-'0'); p++; } return sum; } int main(int argc, char* argv[]){ int def = 1000; if (argc == 2) { def = str2int(argv[argc-1]); } for (int i = 0; i < def ; i++) { float tmp = sqrt(i); } struct rusage usage; getrusage(RUSAGE_SELF, &usage); //getrusage(RUSAGE_CHILDREN, &usage); long user = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec; // user time used long sys = usage.ru_stime.tv_sec * 1e6 + usage.ru_stime.tv_usec; // sys time used printf("User: %ld us\n", user); // 用戶空間使用的時間 printf("Sys: %ld us\n", sys); // 內核空間使用的時間 printf("Total: %ld us\n", user+sys); // 總共使用的時鐘 return 0; }
保持更新,如果對您有幫助請點擊推薦!更多關於Linux 相關的知識,請關註 cnblogs.com/xuyaowen