#include <sys/times.h>
clock_t times(struct tms *buf);
/* Structure describing CPU time used by a process and its children. */
struct tms
{
clock_t tms_utime; /* User CPU time. 用户程序 CPU 时间*/
clock_t tms_stime; /* System CPU time. 系统调用所耗费的 CPU 时间 */
clock_t tms_cutime; /* User CPU time of dead children. 已死掉子进程的 CPU 时间*/
clock_t tms_cstime; /* System CPU time of dead children. 已死掉子进程所耗费的系统调用 CPU 时间*/
};
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/times.h>
int main()
{
struct tms time_buf_head, time_buf_end;
long tck = 0;
clock_t time_head, time_end;
tck = sysconf (_SC_CLK_TCK); /*获取系统时钟(1秒里有多少个)*/
time_head = times( &time_buf_head ); /*进程运行到此时的系统时钟数(总的)*/
printf ("head_time is : %f\\n", time_head / (double)tck); /*此时进程所处的时间点(单位为秒)*/
//system ("./time_test.exe");
system ("sleep 2"); /*睡眠2秒*/
time_end = times( &time_buf_end ); /*进程到此时的系统时钟数*/
printf ("end_time is : %f\\n", time_end / (double)tck); /*此时进程所处的时间点(单位为秒)*/
printf ("user time is : %f\\n", ((time_buf_end.tms_utime - time_buf_head.tms_utime) / (double)tck)); /*打印出用户进程到此所经历时间*/
printf ("systime time is : %f\\n", ((time_buf_end.tms_stime - time_buf_head.tms_stime) / (double)tck));
printf ("child user time is : %f\\n", ((time_buf_end.tms_cutime - time_buf_head.tms_cutime) / (double)tck));
printf ("child sys time is : %f\\n", ((time_buf_end.tms_cstime - time_buf_head.tms_cstime) / (double)tck));
return (0);
}
beyes@beyes-groad:~$ ./time.exe
head_time is : 17236892.770000
end_time is : 17236894.790000
user time is : 0.000000
systime time is : 0.000000
child user time is : 0.000000
child sys time is : 0.000000
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/times.h>
int main()
{
struct tms time_buf_head, time_buf_end;
long tck = 0;
clock_t time_head, time_end;
int i;
int j;
tck = sysconf (_SC_CLK_TCK);
time_head = times( &time_buf_head );
printf ("head_time is : %f\\n", time_head / (double)tck);
/*延迟测试用*/
for (i = 0; i < 20000; i++)
for (j = 0; j < 20000; j++) {
;
}
time_end = times( &time_buf_end );
printf ("end_time is : %f\\n", time_end / (double)tck);
printf ("user time is : %f\\n", ((time_buf_end.tms_utime - time_buf_head.tms_utime) / (double)tck)); /*用户进程所耗费的时间*/
printf ("systime time is : %f\\n", ((time_buf_end.tms_stime - time_buf_head.tms_stime) / (double)tck));
printf ("child user time is : %f\\n", ((time_buf_end.tms_cutime - time_buf_head.tms_cutime) / (double)tck));
printf ("child sys time is : %f\\n", ((time_buf_end.tms_cstime - time_buf_head.tms_cstime) / (double)tck));
return (0);
}
beyes@beyes-groad:~$ ./time.exe
head_time is : 17184643.070000
end_time is : 17184644.280000
user time is : 1.200000
systime time is : 0.000000
child user time is : 0.000000
child sys time is : 0.000000
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/times.h>
int main()
{
struct tms time_buf_head, time_buf_end;
long tck = 0;
clock_t time_head, time_end;
int i;
int j;
tck = sysconf (_SC_CLK_TCK);
time_head = times( &time_buf_head );
printf ("head_time is : %f\\n", time_head / (double)tck);
for (i = 0; i < 1000; i++)
for (j = 0; j < 1000; j++) {
open ("Cannon-1.txt", O_RDONLY);
}
time_end = times( &time_buf_end );
printf ("end_time is : %f\\n", time_end / (double)tck);
printf ("user time is : %f\\n", ((time_buf_end.tms_utime - time_buf_head.tms_utime) / (double)tck));
printf ("systime time is : %f\\n", ((time_buf_end.tms_stime - time_buf_head.tms_stime) / (double)tck));
printf ("child user time is : %f\\n", ((time_buf_end.tms_cutime - time_buf_head.tms_cutime) / (double)tck));
printf ("child sys time is : %f\\n", ((time_buf_end.tms_cstime - time_buf_head.tms_cstime) / (double)tck));
return (0);
}
beyes@beyes-groad:~$ ./time.exe
head_time is : 17189923.210000
end_time is : 17189923.650000
user time is : 0.160000
systime time is : 0.280000
child user time is : 0.000000
child sys time is : 0.000000
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int i;
int j;
for (i = 0; i < 1000; i++)
for (j = 0; j < 1000; j++) {
open ("Cannon-1.txt", O_RDONLY);
}
return (0);
}
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/times.h>
int main()
{
struct tms time_buf_head, time_buf_end;
long tck = 0;
clock_t time_head, time_end;
int i;
int j;
tck = sysconf (_SC_CLK_TCK);
time_head = times( &time_buf_head );
printf ("head_time is : %f\\n", time_head / (double)tck);
system ("./time_test.exe");
time_end = times( &time_buf_end );
printf ("end_time is : %f\\n", time_end / (double)tck);
printf ("user time is : %f\\n", ((time_buf_end.tms_utime - time_buf_head.tms_utime) / (double)tck));
printf ("systime time is : %f\\n", ((time_buf_end.tms_stime - time_buf_head.tms_stime) / (double)tck));
printf ("child user time is : %f\\n", ((time_buf_end.tms_cutime - time_buf_head.tms_cutime) / (double)tck));
printf ("child sys time is : %f\\n", ((time_buf_end.tms_cstime - time_buf_head.tms_cstime) / (double)tck));
return (0);
}
beyes@beyes-groad:~$ ./time.exe
head_time is : 17190766.590000
end_time is : 17190767.060000
user time is : 0.000000
systime time is : 0.000000
child user time is : 0.140000
child sys time is : 0.300000
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |