I want to find the time taken by another program to run ;
I am using following code;
system("time ./a.out > garb");
it is giving very weird output.
#include <stdio.h>
int main()
{
long int i;
for ( i = 0; i < 10000000; i++ ) {
printf("Hello World!\n");
}
printf("C Program\n");
return 0;
}
output
0.31user 0.10system 0:00.41elapsed 99%CPU (0avgtext+0avgdata 1744maxresident)k
0inputs+253912outputs (0major+149minor)pagefaults 0swaps
One way is to use
wait3orwait4functions (if those are available in your system).Your program will get resource usage of a child process after the child is exited.
All fields of
struct rusageare not updated, but the first two fields tells what you want:Sum of
ru_utimeandru_stimeis the total CPU time used by the child process.Using of
wait3 / wait4is not so simple as calling ofsystem()function.EDIT:
You should get same result by summing those two values of printout of
time: