Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get very precise elapsed time C++

I'm running my code on Ubuntu, and I need to get the elapsed time about a function on my program. I need a very accurate time, like nano seconds or at least micro seconds.

I read about chrono.h but it uses system time, and I prefer use CPU time.

Is there a way to do that, and have that granularity (nano seconds)?

like image 366
Teo Avatar asked Oct 18 '16 09:10

Teo


People also ask

How do you calculate elapsed time in CPP?

Since C++11, the best way to measure elapsed time in C++ is by using the Chrono library, which deals with time. Following C++ program calculates the time elapsed for a simple code in seconds, milliseconds, microseconds, and nanoseconds. It includes the <chrono.

What is a elapsed time?

Elapsed time is the time or difference between a beginning time and an ending time. Finding elapsed time is an important skill in everyday life.

How do you calculate elapsed time 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.

Is time H accurate?

It is a system independent C function declared in time. h (compatible on most operationg systems), but it does not give accurate results, not even millisecond accuracy.


2 Answers

std::chrono does have a high_resolution_clock, though please bear in mind that the precision is limited by the processor.

If you want to use functions directory from libc, you can use gettimeofday but as before there is no guarantee that this will be nanosecond accurate. (this is only microsecond accuracy)

like image 61
doron Avatar answered Oct 05 '22 02:10

doron


The achievable precision of the clock is one of the properties of different hardware / OS that still leak into virtually every language, and, to be honest, having been in the same situation I find building yourself an own abstraction that is good enough in your case is often the only choice.

That being said, I would avoid the STL for high-precision timing. Since it is a library standard with no one true implementation, it has to create an abstraction, which implies one of:

  • use a least common denominator
  • leak hardware/OS details through platform-dependent behavior

In the second case you are essentially back to where you started, if you want to have uniform behavior. If you can afford the possible loss of precision or the deviations of a standard clock, then by all means use it. Clocks are hard and subtle.

If you know your target environment you can choose the appropriate clocks the oldschool way (#ifdef PLATFORM_ID...), e.g. clock_gettime(), QPC), and implement the most precise abstraction you can get. Of course you are limited by the same choice the STL has to make, but by reducing the set of platforms, you can generally improve on the lcd-requirement.

If you need a more theoretical way to convince yourself of this argumentation, you can consider the set of clocks with their maximum precision, and a sequence of accesses to the current time. For clocks advancing uniformly in uniform steps, if two accesses happen faster than the maximum precision of one clock, but slower than the maximum precision of another clock, you are bound to get different behavior. If on the other hand you ensure that two accesses are at least the maximum precision of the slowest clock apart the behavior is the same. Now of course real clocks are not advancing uniformly (clock drift), and also not in unit-steps.

like image 31
midor Avatar answered Oct 05 '22 04:10

midor