程序运行时间测试 - 使用系统函数 getrusage 获取程序运行时间
程序员文章站
2022-05-18 21:06:29
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
上一篇: python基础--面向对象之继承