【发布时间】:2015-06-13 02:55:13
【问题描述】:
我有一个名为 IDCreator 的 c++ 应用程序,它需要两个参数来生成 ID。它使用 printf ---- 将其返回值打印到 STDOUT ---- 调用进程从中获取。
现在,我想知道 IDCreator 每秒能够生成多少个 ID(必须在此应用程序之外完成),我该如何实现? 下面的代码适合做这样的工作吗?还有其他方法吗?
string getid;
getid.append("./IDCreator arg1 arg2");
int count = 0;
const int PERIOD = 100;
const int LEN = 512;
char buff[LEN] = {0};
time_t tick1 = time(NULL);
while(1)
{
time_t tick2 = time(NULL);
if ((tick2 - tick1) > PERIOD)
break;
FILE* res = popen(cmd.c_str(), "r");
if (res)
{
fgets(res, buff, sizeof(buf));
pclose(res);
count++;
}
}
printf("Products Per Second is: %d", count/PERIOD);
【问题讨论】:
-
boost:timer是我常用的。为什么它需要在程序之外完成?计时的东西几乎不会影响性能。 -
@twentylemon 因为它被封装在一个 exe 应用程序中,我所知道的只是使用 IPC 的调用。
标签: c++ linux algorithm performance ipc