Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring the runtime of a C++ code?

Tags:

c++

linux

I want to measure the runtime of my C++ code. Executing my code takes about 12 hours and I want to write this time at the end of execution of my code. How can I do it in my code?

Operating system: Linux

like image 508
peaceman Avatar asked Jun 16 '12 10:06

peaceman


People also ask

How do you measure runtime of a program?

The simplest and most intuitive way to measure time is to use a stopwatch manually from the moment you start a program. Assuming you manage to start the stopwatch and the program at the same time, the option requires the program to print a certain warning at the end of execution or at the end of a subroutine.

How do you calculate the execution time of a block of code?

Calculate the execution timeThe difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.


2 Answers

If you are using C++11 you can use system_clock::now():

auto start = std::chrono::system_clock::now();  /* do some work */  auto end = std::chrono::system_clock::now(); auto elapsed = end - start; std::cout << elapsed.count() << '\n'; 

You can also specify the granularity to use for representing a duration:

// this constructs a duration object using milliseconds auto elapsed =     std::chrono::duration_cast<std::chrono::milliseconds>(end - start);  // this constructs a duration object using seconds auto elapsed =     std::chrono::duration_cast<std::chrono::seconds>(end - start); 

If you cannot use C++11, then have a look at chrono from Boost.

The best thing about using such a standard libraries is that their portability is really high (e.g., they both work in Linux and Windows). So you do not need to worry too much if you decide to port your application afterwards.

These libraries follow a modern C++ design too, as opposed to C-like approaches.

EDIT: The example above can be used to measure wall-clock time. That is not, however, the only way to measure the execution time of a program. First, we can distinct between user and system time:

  • User time: The time spent by the program running in user space.
  • System time: The time spent by the program running in system (or kernel) space. A program enters kernel space for instance when executing a system call.

Depending on the objectives it may be necessary or not to consider system time as part of the execution time of a program. For instance, if the aim is to just measure a compiler optimization on the user code then it is probably better to leave out system time. On the other hand, if the user wants to determine whether system calls are a significant overhead, then it is necessary to measure system time as well.

Moreover, since most modern systems are time-shared, different programs may compete for several computing resources (e.g., CPU). In such a case, another distinction can be made:

  • Wall-clock time: By using wall-clock time the execution of the program is measured in the same way as if we were using an external (wall) clock. This approach does not consider the interaction between programs.
  • CPU time: In this case we only count the time that a program is actually running on the CPU. If a program (P1) is co-scheduled with another one (P2), and we want to get the CPU time for P1, this approach does not include the time while P2 is running and P1 is waiting for the CPU (as opposed to the wall-clock time approach).

For measuring CPU time, Boost includes a set of extra clocks:

  • process_real_cpu_clock, captures wall clock CPU time spent by the current process.
  • process_user_cpu_clock, captures user-CPU time spent by the current process.
  • process_system_cpu_clock, captures system-CPU time spent by the current process. A tuple-like class process_cpu_clock, that captures real, user-CPU, and system-CPU process times together.
  • A thread_clock thread steady clock giving the time spent by the current thread (when supported by a platform).

Unfortunately, C++11 does not have such clocks. But Boost is a wide-used library and, probably, these extra clocks will be incorporated into C++1x at some point. So, if you use Boost you will be ready when the new C++ standard adds them.

Finally, if you want to measure the time a program takes to execute from the command line (as opposed to adding some code into your program), you may have a look at the time command, just as @BЈовић suggests. This approach, however, would not let you measure individual parts of your program (e.g., the time it takes to execute a function).

like image 178
betabandido Avatar answered Oct 11 '22 06:10

betabandido


Use std::chrono::steady_clock and not std::chrono::system_clock for measuring run time in C++11. The reason is (quoting system_clock's documentation):

on most systems, the system time can be adjusted at any moment

while steady_clock is monotonic and is better suited for measuring intervals:

Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward. This clock is not related to wall clock time, and is best suitable for measuring intervals.

Here's an example:

auto start = std::chrono::steady_clock::now(); // do something auto finish = std::chrono::steady_clock::now(); double elapsed_seconds = std::chrono::duration_cast<   std::chrono::duration<double> >(finish - start).count(); 

A small practical tip: if you are measuring run time and want to report seconds std::chrono::duration_cast<std::chrono::seconds> is rarely what you need because it gives you whole number of seconds. To get the time in seconds as a double use the example above.

like image 34
vitaut Avatar answered Oct 11 '22 07:10

vitaut