Best accurate way to measure compare elapsed time in C++ : cpp

Every system has different timers and clocks and to improve time with precision we can make use of this library. Create an instance of the duration class with the difference between the start and finish time points recorded from Step 1. As I can see from your question, it looks like you want to know the elapsed time after execution of some piece of code. I guess you would be comfortable to see the results in second. This is because computers are typically non-deterministic, having to deal with interrupts, frequency scaling, and other things that will influence how fast things are running. The function you are calling itself might be non-deterministic. If you are going to record individual time measurements, consider doing some statistics on them.

For example, either the minimum time or the median time or is probably more relevant than the average. You might also want to make the standard deviation available to the user, and maybe the maximum and 95th percentile values as well. Since C++11, the best way to measure elapsed time in C++ is by using the Chrono library, which deals with time. As others have already noted, the time() function in the C standard library does not have a resolution better than one second.

Source Code

On higher-end systems that is simply not the case, mainly due to caching and branch prediction . Hence this type of micro-benchmarking is almost useless. First, the difference between finish and start is computed. Then, this difference is multiplied by a factor of 1,000 to prepare for the final value in milliseconds. Only after this multiplication occurs, the resulting value is divided by the frequency (in ticks-per-second). Following these exact steps helps us to avoid precision losses. Using this method, we record the start and the finish time of the execution.

  • I needed to measure the execution time of individual functions within a library.
  • For starters it doesn’t return the date time, and it won’t always be different.
  • The main difference here is that the returned output parameter is the frequency of the performance counter (which is measured in ticks-per-second).
  • First, the difference between finish and start is computed.
  • What I don’t understand is why the values in the before and after are the same?

This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. I have used hyperfine in the past, a rust CLI based tool to measure performance. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. To start calculating time in C++ with Windows high-resolution time stamps in native user-mode C++ code, there are a couple of Windows APIs you’ll want to note. @nikos-athanasiou I completely agree with the comment from 5gon12eder that “typical” case is not required by the standard so some STL may be implemented in a different way. I prefer to have my code to be more generic and not related on implementation details.


Whilst your answer is appreciated, we do prefer a pre-amble containing a brief description of the code. What I don’t understand is why the values in the before and after are the same? I understand this is not the best way to profile my program, I just want to see how long something take.

c++ measure time

To get a better idea of how this all works, we will start by covering a few useful classes (available since C++11) for dealing with C++ time measurements. Then, we’ll look at how to implement measures using direct calls to Windows APIs. Internally the function will access the system’s clock, which is why it returns different values each time you call it. In general with non-functional languages there can be many side effects and hidden state in functions which you can’t see just by looking at the function’s name and arguments. When programming parallel code for clusters, this method doesn’t reflect the real-world time…

Warm-up, autoscaling, errors

I also didn’t want to put timer code at the top and bottom of every function because it makes a mess when the function can exit early or throw exceptions for example. So what I ended up doing was making a timer that uses its own lifetime to measure time. Here we will measure elapsed time in the C++ program in seconds, milliseconds, microseconds, and nanoseconds. This post will discuss how to measure the elapsed time of a C++ program in seconds, milliseconds, microseconds, and nanoseconds using the Chrono library. After the start and finish times are recorded, the time interval measurement can be done with some simple math.

How do you sleep in milliseconds?

To sleep for milliseconds with this method, simply use a fractional number. To sleep for 400 milliseconds, for example, use time. sleep(0.4), use time for 60 milliseconds sleep(0.06), for example. Python's sleep() function is a part of the time package.

The first question and sample code shows that time() has a resolution of 1 second, so the answer has to be that the two functions execute in less than 1 second. But occasionally it will inform 1 second if the two timer marks straddle a one second boundary. The time() function is only accurate to within a second, but there are CLOCKS_PER_SEC “clocks” within a second.

c++ measure time of function

You can use GetTickCount() to get the number of milliseconds that have elapsed since the system was started. I tried this solution, and as the comments suggested, my timer ran much faster then the real world time. @RestlessC0bra According to the docs on cppreference, “This clock is not related to wall clock time , and is most suitable for measuring intervals.”

c++ measure time

Ticks aren’t all that human-friendly, so we’ll want to measure the elapsed time using other units, like seconds or milliseconds. I needed to measure the execution time of individual functions within a library. I didn’t want to have to wrap every call of every function with a time measuring function because its ugly and deepens the call stack.

Related Posts

Connect and share knowledge within a single location that is structured and easy to search. Asking for help, clarification, or responding to other answers. Discussions, articles and news about the C++ programming language or programming in C++. Thankfully, you can use the C++11’s auto keyword to help reduce that clutter and increase code readability.

Article was published on: 10/21/22

Author: Viktor Nikolaev

Victor is a professional crypto investor and stockbroker, specializing in such areas as trading on the stock exchange, cryptov currencies, forex, stocks and bonds. In this blog he shares the secrets of trading, current currency indices, crypt currency rates and tells about the best forex brokers. If you have any questions, you can always contact

Leave a Reply