Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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

share|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.

Bash:

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
share|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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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