measuring microseconds a process runs in Linux

6,085

Solution 1

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

Solution 2

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:
6,085

Related videos on Youtube

darkastroboy
Author by

darkastroboy

Updated on September 17, 2022

Comments

  • darkastroboy
    darkastroboy almost 2 years

    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

  • Paul R
    Paul R about 14 years
    +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