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

Re: PATCH: SECONDS can be floating point

While I'm at it, this makes a local SECONDS work a bit better... it's
not logically tied to the other patch but it's now more useful to have
the problem fixed, since you might want to use `typeset -F seconds' in a
function to get more precision locally.

The problem was that if you made SECONDS local, then on return to the
parent function, the time spent in the lower function didn't appear in
the value of SECONDS, hence if you were using it for overall timing ---
which is a natural thing to do --- you could miscount completely,
depending arbitrarily on the behaviour of nested functions.  This fixes
the problem by restoring SECONDS to include the elapsed time spent in
the function.  Of course, the accuracy won't be perfect, but it wouldn't
be anyway.

I think there may still be a problem if you do `typeset SECONDS=value'
inside a function that value may remain added in on return but I haven't
looked at this in detail.

Index: Src/params.c
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.67
diff -u -r1.67 params.c
--- Src/params.c	29 Oct 2002 10:31:16 -0000	1.67
+++ Src/params.c	29 Oct 2002 11:10:16 -0000
@@ -3484,7 +3484,24 @@
 	    Param tpm = pm->old;
 	    if (!strcmp(pm->nam, "SECONDS"))
+	    {
 		setsecondstype(pm, PM_TYPE(tpm->flags), PM_TYPE(pm->flags));
+		/*
+		 * We restore SECONDS by adding back in the elapsed
+		 * time (from the point we reset shtimer) rather
+		 * than restoring it completely, since SECONDS should
+		 * run in the calling function, too.
+		 */
+		if (PM_TYPE(pm->flags) == PM_INTEGER)
+		{
+		    pm->sets.ifn(pm, pm->gets.ifn(pm) + tpm->u.val);
+		}
+		else
+		{
+		    pm->sets.ffn(pm, pm->gets.ffn(pm) + tpm->u.dval);
+		}
+		tpm->flags |= PM_NORESTORE;
+	    }
 	    DPUTS(!tpm || PM_TYPE(pm->flags) != PM_TYPE(tpm->flags) ||
 		  !(tpm->flags & PM_SPECIAL),
 		  "BUG: in restoring scope of special parameter");

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070

The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
If you received this in error, please contact the sender and 
delete the material from any computer.

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