Let's suppose that the script generating the output is called generate
. Then, to display the number of seconds it takes for it to generate each line:
$ generate | ( t0=$(date +%s); while read -r line; do t1=$(date +%s); echo " $((t1-t0)) $line"; t0=$t1; done )
2 foo
0 foo
5 foo
3 foo
The same commands spread out over multiple lines looks like:
generate | ( t0=$(date +%s)
while read -r line
do
t1=$(date +%s)
echo " $((t1-t0)) $line"
t0=$t1
done
)
Alternatively, for convenience, we can define a shell function that contains this code:
timer() { t0=$(date +%s); while read -r line; do t1=$(date +%s); echo " $((t1-t0)) $line"; t0=$t1; done; }
We can use this function as follows:
$ generate | timer
0 foo
2 foo
4 foo
3 foo
How it works
t0=$(date +%s)
This captures the current time at the start of the script in seconds-since-epoch.
while read -r line; do
This starts a loop which reads from standard input
t1=$(date +%s)
This captures the time in seconds-since-epoch at which the current line was captured.
echo " $((t1-t0)) $line"
This prints out the time in seconds that it took for the current line.
t0=$t1
This updates t0
for the next line.
done
This signals the end of the while
loop.
time
command does this but I'm not quite sure how to extract it's output. – jesse_b Sep 08 '17 at 19:01