使用system()函数调用子项目二进制文件比链接子项目快4倍

时间:2017-01-17 17:50:36

标签: c++ performance system

我需要从主项目调用子项目并实现两种方法。 事实证明,第二种方式比第一种方式慢4倍。 任何人都可以向我解释一下吗?

子项目如下所示:

#include "fancyProject.h"
int main (int argc, char *argv[])
{
  std::string controlFile = argv[1];
  return runFancyProject(controlFile);
}

第一种方法:通过system()函数调用主项目中子项目的二进制文件:

std::string command = "fancyProject controlFile.dat";
int result = system(command.c_str());

第二种方式:从子项目创建库,将该库与主项目链接并调用特定函数:

#include "fancyProject.h"
std::string controlFile = "controlFile.dat";
int result = runFancyProject(controlFile);

与此同时,我创建了一个minimal example。但是,它的行为与预期的一样:system()函数比链接函数的调用慢。因此,错误必须在项目的其他地方。不过非常感谢您的时间,特别是对于dsboger。我会朝这个方向做进一步的调查。

0 个答案:

没有答案