如何在Linux / Windows上测量CPU时间和挂钟时间?

我的意思是:如何测量我的CPU花费在函数执行上的时间以及运行我的函数所需的挂钟时间? (我对Linux / Windows以及x86和x86_64感兴趣)。 看看我想做什么(我在这里使用C ++,但我更喜欢C解决scheme):

int startcputime, endcputime, wcts, wcte; startcputime = cputime(); function(args); endcputime = cputime(); std::cout << "it took " << endcputime - startcputime << " s of CPU to execute this\n"; wcts = wallclocktime(); function(args); wcte = wallclocktime(); std::cout << "it took " << wcte - wcts << " s of real time to execute this\n"; 

另一个重要的问题是:这种types的时间测量架构是否独立?

这是一个可在Windows和Linux以及C和C ++上工作的复制粘贴解决scheme。

正如在评论中提到的,有一个提升库这样做。 但是,如果你不能使用提升,这应该工作:

 // Windows #ifdef _WIN32 #include <Windows.h> double get_wall_time(){ LARGE_INTEGER time,freq; if (!QueryPerformanceFrequency(&freq)){ // Handle error return 0; } if (!QueryPerformanceCounter(&time)){ // Handle error return 0; } return (double)time.QuadPart / freq.QuadPart; } 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; } } // Posix/Linux #else #include <time.h> #include <sys/time.h> double get_wall_time(){ struct timeval time; if (gettimeofday(&time,NULL)){ // Handle error return 0; } return (double)time.tv_sec + (double)time.tv_usec * .000001; } double get_cpu_time(){ return (double)clock() / CLOCKS_PER_SEC; } #endif 

有很多方法来实现这些时钟。 但是,这是以上代码片段的用途:

对于Windows:

  • 墙上时间: 性能计数器
  • CPU时间: GetProcessTimes()

对于Linux:

  • 墙上时间: gettimeofday()
  • CPU时间: clock()

这是一个小示范:

 #include <math.h> #include <iostream> using namespace std; int main(){ // Start Timers double wall0 = get_wall_time(); double cpu0 = get_cpu_time(); // Perform some computation. double sum = 0; #pragma omp parallel for reduction(+ : sum) for (long long i = 1; i < 10000000000; i++){ sum += log((double)i); } // Stop timers double wall1 = get_wall_time(); double cpu1 = get_cpu_time(); cout << "Wall Time = " << wall1 - wall0 << endl; cout << "CPU Time = " << cpu1 - cpu0 << endl; // Prevent Code Elimination cout << endl; cout << "Sum = " << sum << endl; } 

输出(12个线程):

 Wall Time = 15.7586 CPU Time = 178.719 Sum = 2.20259e+011 

C ++ 11。 写起来容易多了!

使用std::chrono::system_clock挂钟和std::clock作为CPU时钟http://en.cppreference.com/w/cpp/chrono/system_clock

 #include <cstdio> #include <ctime> #include <chrono> .... std::clock_t startcputime = std::clock(); do_some_fancy_stuff(); double cpu_duration = (std::clock() - startcputime) / (double)CLOCKS_PER_SEC; std::cout << "Finished in " << cpu_duration << " seconds [CPU Clock] " << std::endl; auto wcts = std::chrono::system_clock::now(); do_some_fancy_stuff(); std::chrono::duration<double> wctduration = (std::chrono::system_clock::now() - wcts); std::cout << "Finished in " << wctduration.count() << " seconds [Wall Clock]" << std::endl; 

等voilà,轻松便携! 不需要#ifdef _WIN32或LINUX!

你甚至可以使用chrono::high_resolution_clock如果你需要更多的精度http://en.cppreference.com/w/cpp/chrono/high_resolution_clock

如果可以(用Boost 1.51进行testing)给出一个@ lipbuild议的具体例子来使用boost::timer

 #include <boost/timer/timer.hpp> // this is wallclock AND cpu time boost::timer::cpu_timer timer; ... run some computation ... boost::timer::cpu_times elapsed = timer.elapsed(); std::cout << " CPU TIME: " << (elapsed.user + elapsed.system) / 1e9 << " seconds" << " WALLCLOCK TIME: " << elapsed.wall / 1e9 << " seconds" << std::endl; 

在time.h中使用clock方法:

 clock_t start = clock(); /* Do stuffs */ clock_t end = clock(); float seconds = (float)(end - start) / CLOCKS_PER_SEC; 

不幸的是,这种方法在Linux上返回CPU时间,但在Windows上返回挂钟时间 (感谢这些信息的评论者)。