简体   繁体   中英

Measure time on C++ for system call command in Linux

I am trying to measure the cpu time of an external software that I call from my C++ code with system (in Linux). I wonder if the "getusage user and system time" can be compare with the "user and system time given by the time command".

Example, would the time returned by these two pieces of code be (approximately) the same, that is, would I be doing a fair comparison?:

//CODE 1 (GETUSAGE)

long int timeUsage1,timeUsage2 = 0;
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
timeUsage1 = usage.ru_utime.tv_sec+usage.ru_stime.tv_sec;
//C++ code
getrusage(RUSAGE_SELF, &usage);
timeUsage2 = ((usage.ru_utime.tv_sec+usage.ru_stime.tv_sec)-timeUsage1);

//CODE 2 (TIME LINUX COMMAND from my C++ code)

system(time external) //where external is equivalent to C++ code above

Thanks, Ana

PS: With the time command from CODE 2 I get something like this:

4.89user 2.13system 0:05.11elapsed 137%CPU (0avgtext+0avgdata 23968maxresident)k 0inputs+86784outputs (0major+2386minor)pagefaults 0swaps

Should I be concerned about the 137%CPU at all?

So, the 137% is based on the fact that 7.02s (total "User + System"), is 137% of the actual time it took to run the code, 5.11s. So, if you had only one processor (core), the overall time would be at least 7.02s.

Since we don't see your actual code, it's hard to say if this is caused by your code being multithreaded, or if the time is spent in the kernel that runs things in multiple threads "behind the scenes" so to speak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM