help-bash
[Top][All Lists]
Advanced

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

Re: SECONDS=0 does not reset SECONDS, or I'm missing something


From: Tim Visher
Subject: Re: SECONDS=0 does not reset SECONDS, or I'm missing something
Date: Thu, 4 Jun 2020 12:44:18 -0400

On Thu, Jun 4, 2020 at 10:46 AM Andreas Kusalananda Kähäri <
andreas.kahari@abc.se> wrote:

> On Thu, Jun 04, 2020 at 09:58:39AM -0400, Greg Wooledge wrote:
> > On Thu, Jun 04, 2020 at 03:52:50PM +0200, Andreas Kusalananda Kähäri
> wrote:
> > > I might have been a bit imprecise in describing my issue.
> > >
> > > Most of the time, removing the directories takes less than a second.
> > > For these cases, I don't want to execute the sleep at all.  Only when a
> > > call to rm takes more than one second do I want to sleep.
> >
> > So, use "time rm ..." with the TIMEFORMAT variable set how you like.
> > Capture the output of time into a variable (trickier than it sounds),
> > and then sleep only if the variable does not match 0.* .
> >
> > See <https://mywiki.wooledge.org/BashFAQ/032> for help capturing time's
> > output.
>
> This is most useful, thanks!  Yes, getting the time out of the time
> keyword is a bit of a hassle, but it's good to see that it's doable, and
> my initial tests works as expected.
>
> I might possible submit a documentation patch against bash later, if I
> remember, or I'll add it to my long list of TODO items, for a rainier
> day.
>

Definitely give what Greg suggested a try. I'm also quite surprised by this
behavior. I did a little experimentation with it and the behavior of the
SECONDS variable almost feels non-deterministic. I ran 3 experiments in
bash and zsh. zsh behaved as intuitively expected every time and bash
exhibited the behavior you're describing in increasingly rare cases but
even at sleeping for 0.001 seconds it still occasionally showed that a
second had passed. I wonder if floating point rounding errors could be to
blame? Just figured I'd share.

```
# bash --version && for s in 0.1 0.01 0.001; do echo "$s"; for run in
{1..1000}; do ( SECONDS=0; echo -n "$SECONDS"; sleep "$s"; echo
"$SECONDS"); done | sort | uniq -c; done
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-musl)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
0.1
    885 00
    115 01
0.01
    986 00
     14 01
0.001
    996 00
      4 01

# zsh --version; for run in {1..1000}; do ( SECONDS=0; echo -n "$SECONDS";
sleep 0.01; echo "$SECONDS"); done | sort | uniq -c
zsh 5.8 (x86_64-pc-linux-gnu)
   1000 00
e39ae351e1eb# zsh --version; for s in 0.1 0.01 0.001; do echo "$s"; for run
in {1..1000}; do ( SECONDS=0; echo -n "$SECONDS"; sleep "$s"; echo
"$SECONDS"); done | sort | uniq -c; done
zsh 5.8 (x86_64-pc-linux-gnu)
0.1
   1000 00
0.01
   1000 00
0.001
   1000 00
```


reply via email to

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