Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] issues with $SECONDS setting and expansio



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;
 }
 
 /**/



Messages sorted by: Reverse Date, Date, Thread, Author