gpt4 book ai didi

c++ - 实时、用户时间和系统时间的区别

转载 作者:太空狗 更新时间:2023-10-29 23:22:55 30 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
What specifically are wall-clock-time, user-cpu-time, and system-cpu-time in UNIX?

以下程序将运行一个命令,然后显示有关它使用了多少时间的各种值:

#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("\ncommand: %s\n", 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\n", real / (double) clktck);
printf(" user: %7.2f\n",
(tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck);
printf(" sys: %7.2f\n",
(tmsend->tms_stime - tmsstart->tms_stime) / (double)
clktck);
printf(" child user: %7.2f\n",
(tmsend->tms_cutime - tmsstart->tms_cutime) /
(double) clktck);
printf(" child sys: %7.2f\n",
(tmsend->tms_cstime - tmsstart->tms_cstime) /
(double) clktck);
}

这是一个输出示例:

 real:     1.44
user: 0.00
sys: 0.00
child user: 0.03
child sys: 0.00

实际时间是总共经过了多少时间,对吗?其他值是什么?

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com