Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring execution time of a function in C++

I want to find out how much time a certain function takes in my C++ program to execute on Linux. Afterwards, I want to make a speed comparison . I saw several time function but ended up with this from boost. Chrono:

process_user_cpu_clock, captures user-CPU time spent by the current process 

Now, I am not clear if I use the above function, will I get the only time which CPU spent on that function?

Secondly, I could not find any example of using the above function. Can any one please help me how to use the above function?

P.S: Right now , I am using std::chrono::system_clock::now() to get time in seconds but this gives me different results due to different CPU load every time.

like image 242
Xara Avatar asked Mar 13 '14 18:03

Xara


People also ask

How do you find the time taken to execute a function in C?

To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.

How does time () function work in C?

time() function in C The time() function is defined in time. h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.

How do you get the execution time in VS code?

On Linux and MacOS, you can use time before your command, e.g. time sleep 2 . On Windows 10, it seems to be Measure-Command . Also, on Linux and MacOS at least, you can customize your shell prompt to display the execution time after each command. You can do it manually or use a package like Starship or Powerlevel10k.


1 Answers

It is a very easy-to-use method in C++11. You have to use std::chrono::high_resolution_clock from <chrono> header.

Use it like so:

#include <chrono>  /* Only needed for the sake of this example. */ #include <iostream> #include <thread>      void long_operation() {     /* Simulating a long, heavy operation. */      using namespace std::chrono_literals;     std::this_thread::sleep_for(150ms); }  int main() {     using std::chrono::high_resolution_clock;     using std::chrono::duration_cast;     using std::chrono::duration;     using std::chrono::milliseconds;      auto t1 = high_resolution_clock::now();     long_operation();     auto t2 = high_resolution_clock::now();      /* Getting number of milliseconds as an integer. */     auto ms_int = duration_cast<milliseconds>(t2 - t1);      /* Getting number of milliseconds as a double. */     duration<double, std::milli> ms_double = t2 - t1;      std::cout << ms_int.count() << "ms\n";     std::cout << ms_double.count() << "ms\n";     return 0; } 

This will measure the duration of the function long_operation.

Possible output:

150ms 150.068ms 

Working example: https://godbolt.org/z/oe5cMd

like image 52
Victor Avatar answered Sep 27 '22 02:09

Victor