zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] issues with $SECONDS setting and expansio
Date: Wed, 24 Feb 2016 17:09:04 +0000	[thread overview]
Message-ID: <20160224170904.GA12099@chaz.gmail.com> (raw)

In, intsecondsgetfn, 
(zlong)(now.tv_usec - shtimer.tv_usec) / (zlong)1000000

was always 0 because that's the difference between 2 numbers
that are both under 1000000 divided by 1000000.

Also, by setting shtimer.tv_usec to 0 instead of now.tv_usec,
we're arbitrarily adding up to 1 second to $SECONDS.

Before this patch:

$ time zsh -c 'while ((SECONDS < 1)); do :; done'
zsh -c 'while ((SECONDS < 1)); do :; done'  0.20s user 0.07s system 99% cpu 0.269 total

(SECONDS gets incremented in between 0 and 1 seconds after
startup, above 0.269 seconds. bash and mksh have similar issues
which I've also reported there).

$ zsh -c 'SECONDS=0; typeset -F SECONDS; echo $SECONDS'
0.8081650000

After the patch:

$ time ./zsh -c 'while ((SECONDS < 1)); do :; done'
./zsh -c 'while ((SECONDS < 1)); do :; done'  0.87s user 0.13s system 99% cpu 1.003 total

SECONDS reaches 1 after 1 second, consistently.

$ ./zsh -c 'SECONDS=0; typeset -F SECONDS; echo $SECONDS'
0.0000740000

diff --git a/Src/params.c b/Src/params.c
index 8bd8a8e..7c5f79f 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -3804,8 +3804,8 @@ intsecondsgetfn(UNUSED(Param pm))
 
     gettimeofday(&now, &dummy_tz);
 
-    return (zlong)(now.tv_sec - shtimer.tv_sec) +
-	(zlong)(now.tv_usec - shtimer.tv_usec) / (zlong)1000000;
+    return (zlong)(now.tv_sec - shtimer.tv_sec -
+		  (now.tv_usec < shtimer.tv_usec ? 1 : 0));
 }
 
 /* Function to set value of special parameter `SECONDS' */
@@ -3823,7 +3823,7 @@ intsecondssetfn(UNUSED(Param pm), zlong x)
     shtimer.tv_sec = diff;
     if ((zlong)shtimer.tv_sec != diff)
 	zwarn("SECONDS truncated on assignment");
-    shtimer.tv_usec = 0;
+    shtimer.tv_usec = now.tv_usec;
 }
 
 /**/


                 reply	other threads:[~2016-02-24 18:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160224170904.GA12099@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).