Path: csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Stephane Chazelas Newsgroups: gnu.bash.bug Subject: [repost] "precision" of $SECONDS Date: Mon, 16 Sep 2019 19:29:56 +0100 Lines: 39 Approved: bug-bash@gnu.org Message-ID: References: <20160224151641.GB8566@chaz.gmail.com> <20190916182956.pknvg574u3gjp7cm@chaz.gmail.com> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1568658616 21392 209.51.188.17 (16 Sep 2019 18:30:16 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=oxc+ggw6kFpN1Ohah5jp63Jun6hqqoMR5JoChwLk2UQ=; b=HCUzVm80dKlzqr/yBI36KsHTM192GntNsi8DXMJt1033s2Epbba/RPORcSAaonj6bX fjdumDZsRM0dnzqUT9U36UrDGlTioWXIwEun7Q9Ryt2zgUOBDC1GlZ8zmTewt0GhvYS3 VAF0ibMm29qGKx59g8yXuQcc87RZwQebHwcsKEwmVZlnLBhCwF+NpsId5DWo2Cf5vg7z /HG3/vcHHATkyIR+IQRGjAfPssq8q/pRdt81kBtMuFGqdUVABN59qS3V3O0pAGNjn2VJ KI1FX3N+LR+eQWCkrdhMxrigStnDp8A0iB31F7KhKqtrC3t2q9p+cP38T91nQRmFVJIY Hx7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=oxc+ggw6kFpN1Ohah5jp63Jun6hqqoMR5JoChwLk2UQ=; b=oedkw7zFzc0rqgf9XC/JkbcBe8JzFUakSk6Eqt8pGH0HO4rk7UTuWkN+zolVea/v4Z z6wxI2qxicUKpeLSVAbowVomZvSPOncMYd1U5hLzcGC++FXznAj07t7el+HDOg/rhlao fn8tSDccAPMCJr8UXl3DsreoyCloKUZnG60mwShj6+b7QV+E0cjaNu1ZjNuDV3e4WrG/ KKt7wWIxe0so//EWB3XlT73dnxg+Edg4d6e7+fSDNUMc0FW1Yfsg9usQF8Iuy6v5VXXE AnoXaPRC8EqVaM5TuaQuWJ1msIyRPO1ZCnbnX0ChPUjnqlmAtCsKLnAq0yLBhMuaVG8V cGPg== X-Gm-Message-State: APjAAAWL2ISsOQVZviSKU0BTbyjdJFDjUo321sCkgzKN0mjbsuWBKY6c 2pY4O/shO1U5oSGUMopwG83WuM8Y X-Google-Smtp-Source: APXvYqw91Yv9gC4cTfzKqVjn8NnJnPwnY90oQZ6NtfZi/85pS4rZFN3pyt0Rz0mM+C+pOuhhCF/nww== X-Received: by 2002:adf:db0f:: with SMTP id s15mr891692wri.120.1568658599019; Mon, 16 Sep 2019 11:29:59 -0700 (PDT) Content-Disposition: inline In-Reply-To: <20160224151641.GB8566@chaz.gmail.com> User-Agent: NeoMutt/20171215 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::435 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20190916182956.pknvg574u3gjp7cm@chaz.gmail.com> X-Mailman-Original-References: <20160224151641.GB8566@chaz.gmail.com> Xref: csiph.com gnu.bash.bug:15377 That's a a re-post of a bug report I raised a few years ago and comes back now and then in various Q&A sites I participate in. The discussions kind of trailed off last time. https://www.mail-archive.com/bug-bash@gnu.org/msg17783.html 2016-02-24 15:16:41 +0000, Stephane Chazelas: > $ time bash -c 'while ((SECONDS < 1)); do :; done' > bash -c 'while ((SECONDS < 1)); do :; done' 0.39s user 0.00s system 99% cpu 0.387 total > > That can take in between 0 and 1 seconds. Or in other words, > $SECONDS becomes 1 in between 0 and 1 second after the shell was > started. > > The reason seems to be because the shell records the value > returned by time() upon start-up and $SECONDS expands to > time()-that_saved_time. So, if bash is started at 10:00:00.999, > then $SECONDS will become 1 only a milisecond after startup > while if it's started at 10:00:01.000, $SECONDS will become 1 a > full second later. > > IMO, it would be better if gettimeofday() or equivalent was used > instead of time() so that $SECONDS be incremented exactly one > second after start-up like ksh93 does. > > mksh and zsh behave like bash (I'll raise the issue there as > well). > > With zsh (like in ksh93), one can do "typeset -F SECONDS" to > make $SECONDS floating point, which can be used as a work around > of the "issue". [...] Note that since then the corresponding bugs in mksh and zsh have been fixed. -- Stephane