Differences between real time, user time, and system time
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
What specifically are wall-clock-time, user-cpu-time, and system-cpu-time in UNIX?
以下程序将运行命令,然后显示有关使用时间的各种值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <iostream> #include <sys/times.h> using namespace std; static void pr_times(clock_t, struct tms *, struct tms *); static void do_cmd(char *); int main(int argc, char *argv[]) { int i; setbuf(stdout, NULL); for (i = 1; i < argc; i++) do_cmd(argv[i]); /* once for each command-line arg */ exit(0); } static void do_cmd(char *cmd) /* execute and time the"cmd" */ { struct tms tmsstart, tmsend; clock_t start, end; int status; printf(" command: %s ", cmd); if ((start = times(&tmsstart)) == -1) /* starting values */ cout <<"times error" << endl; if ((status = system(cmd)) < 0) /* execute command */ cout <<"system error" << endl; if ((end = times(&tmsend)) == -1) cout <<"times error" << endl; pr_times(end-start, &tmsstart, &tmsend); exit(status); } static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend) { static long clktck = 0; if (clktck == 0) /* fetch clock ticks per second first time */ if ((clktck = sysconf(_SC_CLK_TCK)) < 0) cout <<"sysconf error" << endl; printf(" real: %7.2f ", real / (double) clktck); printf(" user: %7.2f ", (tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck); printf(" sys: %7.2f ", (tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck); printf(" child user: %7.2f ", (tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck); printf(" child sys: %7.2f ", (tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck); } |
这是输出的一个例子:
1 2 3 4 5 | real: 1.44 user: 0.00 sys: 0.00 child user: 0.03 child sys: 0.00 |
实际时间是总时间已经过去了,对吗? 还有什么其他价值?
实时是由挂钟测量的时间,即从产生该过程到结束的时刻之间的时间。用户时间是用户模式下进程花费的CPU时间,sys是执行进程请求的操作时在操作系统中花费的CPU时间。
提供一些不同的例子,当进程调用