* [PATCH] issues with $SECONDS setting and expansio
@ 2016-02-24 17:09 Stephane Chazelas
0 siblings, 0 replies; only message in thread
From: Stephane Chazelas @ 2016-02-24 17:09 UTC (permalink / raw)
To: Zsh hackers list
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;
}
/**/
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2016-02-24 18:11 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-24 17:09 [PATCH] issues with $SECONDS setting and expansio Stephane Chazelas
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).