I mean: how can I measure time my CPU spent on function execution and wall clock time it takes to run my function? (Im interested in Linux/Windows and both x86 and x86_64). See what I want to do (Im using C++ here but I would prefer C solution):
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";
Another important question: is this type of time measuring architecture independent or not?
Wall clock time vs. CPU time. Wall clock time measures how much time has passed, as if you were looking at the clock on your wall. CPU time is how many seconds the CPU was busy.
wtime, a C++ code which returns a reading of the wall clock time. For parallel programming, the important thing to measure is the elapsed wallclock time. This can be found by subtracting an initial reading of the wallclock time from a final one.
Here's a copy-paste solution that works on both Windows and Linux as well as C and C++.
As mentioned in the comments, there's a boost library that does this. But if you can't use boost, this should work:
// 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
There's a bunch of ways to implement these clocks. But here's what the above snippet uses:
For Windows:
GetProcessTimes()
For Linux:
gettimeofday()
clock()
And here's a small demonstration:
#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; }
Output (12 threads):
Wall Time = 15.7586 CPU Time = 178.719 Sum = 2.20259e+011
C++11. Much easier to write!
Use std::chrono::system_clock
for wall clock and std::clock
for cpu clock 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;
Et voilà, easy and portable! No need for #ifdef _WIN32 or LINUX!
You could even use chrono::high_resolution_clock
if you need more precision http://en.cppreference.com/w/cpp/chrono/high_resolution_clock
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With