help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Duration between two time stamps


From: Koichi Murase
Subject: Re: Duration between two time stamps
Date: Thu, 19 Aug 2021 11:23:22 +0900

2021年8月19日(木) 10:17 hancooper via <help-bash@gnu.org>:
> How can I use two time stamps

In Bash 5.0+, you may use the special shell variables, EPOCHSECONDS
(for granularity of seconds) or EPOCHREALTIME (for finer granularity).

A=$EPOCHREALTIME
your_search_command
B=$EPOCHREALTIME

> and compute the elapsed time?

If you don't have decimal parts, you can just compute it as $((B-A)).
If you have decimal parts, you may use `bc' as Seth has replied. Or,
to reduce the spawn cost, I usually do something like the following
way:

sec=$((${B%%.*}-${A%%.*}))
A=${A#*.}000000; A=${A::6}
B=${B#*.}000000; B=${B::6}
usec=$((10#$B-10#$A))
echo $((sec*1000000+usec))

> On Thursday, August 19, 2021 2:01 AM, Seth David Schoen <schoen@loyalty.org> 
> wrote:
> > $ echo "$B-$A" | bc
> > 4.21694
> >
> > This is slower because it creates a subprocess to perform the arithmetic
> > calculation.

In Bash,

bc <<< "$B-$A"

should be a little bit faster.

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]