我应该添加一个小例子。这是我的get_cpu_time函数(来自上面描述的链接):
double get_cpu_time(){
FILETIME a,b,c,d;
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)(d.dwLowDateTime |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
}else{
// Handle error
return 0;
}
}
到目前为止,这很好用,当我制作一个程序来排序一些数组(或者进行其他需要一些时间的操作)时,它也很好用。然而,当我使用system()命令时,就像在这种情况下一样,它就不起作用了:
int main( int argc, const char* argv[] )
{
double start = get_cpu_time();
double end;
system("Bla.exe");
end = get_cpu_time();
printf("Everything took %f seconds of CPU time", end - start);
std::cin.get();
}
给定的exe文件的执行时间以相同方式测量,大约需要5秒钟。当我通过system()运行它时,整个过程需要0秒的CPU时间,这显然不包括exe文件的执行。
一个可能的方法是在系统调用上获得HANDLE,有什么办法可以做到吗?