Microsecond accurate (or better) process timing in Linux
If you are looking for this level of timing resolution, you are probably trying to do some micro-optimization. If that's the case, you should look at PAPI. Not only does it provide both wall-clock and virtual (process only) timing information, it also provides access to CPU event counters, which can be indispensable when you are trying to improve performance.
http://icl.cs.utk.edu/papi/
Linux' hrtimer - microsecond precision?
You can do what you want from user space
- use
clock_gettime()
withCLOCK_REALTIME
to get the time-of-day withnano
-second resolution - use
nanosleep()
to yield the CPU until you are close to the time you need to execute your task (it is at leastmilli
-second resolution). - use a spin loop with
clock_gettime()
until you reach the desired time - execute your task
The clock_gettime()
function is implemented as a VDSO
in recent kernels and modern x86 processors - it takes 20-30 nano
seconds to get the time-of-day with nano
-second resolution - you should be able to call clock_gettime()
over 30 times per micro
-second. Using this method your task should dispatch within 1/30th of a micro
-second of the intended time.
Getting millisecond or microsecond-accurate boot time in Linux (C/C++)
Use clock_gettime(CLOCK_BOOTTIME, &ts).
Is gettimeofday() guaranteed to be of microsecond resolution?
Maybe. But you have bigger problems. gettimeofday()
can result in incorrect timings if there are processes on your system that change the timer (ie, ntpd). On a "normal" linux, though, I believe the resolution of gettimeofday()
is 10us. It can jump forward and backward and time, consequently, based on the processes running on your system. This effectively makes the answer to your question no.
You should look into clock_gettime(CLOCK_MONOTONIC)
for timing intervals. It suffers from several less issues due to things like multi-core systems and external clock settings.
Also, look into the clock_getres()
function.
Getting an accurate execution time in C++ (micro seconds)
If you are using c++11 or later you could use std::chrono::high_resolution_clock
.
A simple use case :
auto start = std::chrono::high_resolution_clock::now();
...
auto elapsed = std::chrono::high_resolution_clock::now() - start;
long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(
elapsed).count();
This solution has the advantage of being portable.
Beware that micro-benchmarking is hard. It's very easy to measure the wrong thing (like your benchmark optimizing away), or to include page-faults in your timed region, or fail to account for CPU frequency idle vs. turbo.
See Idiomatic way of performance evaluation? for some general tips, e.g. sanity check by testing the other one first and see if that changes which one appears faster.
High precision timing in userspace in Linux
clock_gettime
allows you to get a nanosecond-precise time from the thread start, process start or epoch.
Related Topics
How to Connect to the Fitbit Zip Over Bluetooth 4.0 Le on Linux with Bluez
Can't Get Private Key with Openssl (No Start Line:Pem_Lib.C:703:Expecting: Any Private Key)
How to Install a Rpm Package and Its Dependencies Offline
Behavior of Cd/Bash on Symbolic Links
Accessing .So Libraries Using Dlopen() Throws Undefined Symbol Error
.Bashrc Not Read When Shell Script Is Invoked from Desktop Shortcut
Zsh Menu Completion Causes Problems After Zle Reset-Prompt
Force a Shell Script to Fflush
Which Segments Are Affected by a Copy-On-Write
Get Lines of File1 Which Are Not in File2
Renaming a Set of Files to 001, 002,
Run a Persistent Process via Ssh
Sed Help: Matching and Replacing a Literal "\N" (Not the Newline)
Bash: Delete Based on File Date Stamp
Tracking Threads Memory and CPU Consumption
Npm Can't Install Appjs. Error: Cannot Find Module 'Graceful-Fs'