#include <stdio.h> #include <sys/times.h> #include <unistd.h> int main(){ struct tms t; times(&t); long clk_tck = sysconf(_SC_CLK_TCK); printf("In point a ,tms_utime is %ld , tms_stime is %ld , user cpu time is %.2f s , system cpu time is %.2f s\n",t.tms_utime,t.tms_stime,\ (double )t.tms_utime/clk_tck,(double )t.tms_stime/clk_tck); sleep(5); for(int i = 100000000;i>0;i--){ getpid(); } times(&t); printf("In point b ,tms_utime is %ld , tms_stime is %ld , user cpu time is %.2f s , system cpu time is %.2f s\n",t.tms_utime,t.tms_stime,\ (double )t.tms_utime/clk_tck,(double )t.tms_stime/clk_tck); return 0; }
#include <stdio.h> #include <time.h> #include <unistd.h> int main(){ clock_t t; t = clock(); printf("In point a, clock_t is %ld, second is %.2f s\n",t,(double )t/CLOCKS_PER_SEC); for(int i = 100000000;i>0;i--){ getpid(); } t = clock(); printf("In point b, clock_t is %ld, second is %.2f s\n",t,(double )t/CLOCKS_PER_SEC); return 0; }