I'm looking to get the number of microseconds that a process takes to execute. Does anybody know how to do this on a Linux system? (I'm looking for a time value more accurate than milliseconds, because I'm measuring something very sensitive.)

NOTE: I don't think the time command will work to the accuracy I'm looking for... it seems to only go down to milliseconds, which isn't really good enough for my purposes


You should take a look at PAPI. btw, there's a similar question.

| improve this answer | |

You don't say whether this is your own code and you want to time some part of it internally. You also don't say what it is you're actually trying to accomplish.

One thing you might be doing is running some program in the shell. If so, the most straightforward way is to time a loop of multiple calls to the process and do the math.


t=($({ time -p for i in {1..1000000}; do some_prog; done; } 2>&1))
echo "scale=6;${t[1]}/1000000" | bc

If your date command (and system) support nanoseconds and you don't mind the overhead:

begin=$(date +%s.%N); some_prog; end=$(date +%s.%N); echo "scale=6; $end - $begin" | bc
| improve this answer | |
  • +1: this is a much better solution than trying to precisely time an individual run, as it averages out lots of potential sources of error which would otherwise make a sub-millisecond timing unreliable – Paul R Apr 5 '10 at 10:31

times(2) returns the process times in jiffies, not time. As such it can only measure units of time as finely as the frequency of your kernel. clock_gettime(2) can do finer resolutions if the kernel is compiled properly, but there's no easy-to-use userland interface to it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.