简体   繁体   English

在C ++上为Linux中的系统调用命令测量时间

[英]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). 我正在尝试测量我从C ++代码与系统 (在Linux中)调用的外部软件的cpu时间。 I wonder if the "getusage user and system time" can be compare with the "user and system time given by the time command". 我想知道“ getusage用户和系统时间”是否可以与“ time命令给定的用户和系统时间”进行比较。

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) //代码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) //代码2(来自我的C ++代码的时间LINUX命令)

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: PS:使用CODE 2中的time命令,我得到的是这样的:

4.89user 2.13system 0:05.11elapsed 137%CPU (0avgtext+0avgdata 23968maxresident)k 0inputs+86784outputs (0major+2386minor)pagefaults 0swaps 4.89用户2.13系统0:05.11经过了137%CPU(0avgtext + 0avgdata 23968maxresident)k 0输入+86784输出(0major + 2386minor)页面故障0交换

Should I be concerned about the 137%CPU at all? 我应该担心137%的CPU吗?

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. 因此,这137%是基于7.02秒(“用户+系统”总数)占运行代码实际时间5.11秒的137%的事实。 So, if you had only one processor (core), the overall time would be at least 7.02s. 因此,如果您只有一个处理器(核心),则总时间将至少为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. 由于我们看不到您的实际代码,因此很难说这是否是由于您的代码是多线程的,或者是否花了时间在内核中运行了“后台”的多个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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