zsh-workers
 help / color / mirror / code / Atom feed
* [RFC] Millisecond and microsecond specifiers for TIMEFMT
@ 2017-12-12  1:58 dana
  2017-12-13 18:01 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: dana @ 2017-12-12  1:58 UTC (permalink / raw)
  To: zsh-workers

Hey again.

I am often interested in getting sub-second-precision timing on shell commands,
but the `time` built-in doesn't offer this (not really, anyway). I can kind of
work around it by writing a function that fiddles with SECONDS, but my function
doesn't have the fancy syntax support that `time` has, which makes it
inconvenient for measuring some things.

What i'd really like is if TIMEFMT supported sequences for milliseconds and
microseconds.

I had a look and it really does seem quite simple to implement. How would you
feel about something like the change below?

The final result:

  % Src/zsh -c 'TIMEFMT="%E %mE %uE"; time sleep 1.5'
  1.51s 1505ms 1505010us

Issues i can think of:

* Obviously this ties up %m and %u; they can't be used for much else after this.
  TIMEFMT sequences don't seem to change often, so i doubt that's a massive
  problem, but there are other options ofc. Could make it an extension of the %*
  feature, for example — so like %*mE / %*uE. Kind of weird, but it wouldn't
  consume two extra characters at least. Another option might be some kind of
  arbitrary multiplier digit like %*3E / %*6E. Open to ideas.

* Whatever new sequence is chosen, if anyone was relying on its previous
  undefinedness to produce a literal, they will have to update their TIMEFMT
  now. Oh well.

* There might be some loss of precision...? Not that worried about it though,
  it's certainly close enough for my purposes.

* It would be nice if it could actually print µs instead of us, but i assume
  that would lead down a rabbit hole of locale stuff that i don't really know
  how to deal with in C.

dana


diff --git a/Src/jobs.c b/Src/jobs.c
index 226e7cff3..fc5bfacc6 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -728,6 +728,40 @@ printtime(struct timeval *real, child_times_t *ti, char *desc)
 	    case 'S':
 		fprintf(stderr, "%4.2fs", system_time);
 		break;
+	    case 'm':
+		switch (*++s) {
+		case 'E':
+		    fprintf(stderr, "%0.fms", elapsed_time * 1000.0);
+		    break;
+		case 'U':
+		    fprintf(stderr, "%0.fms", user_time * 1000.0);
+		    break;
+		case 'S':
+		    fprintf(stderr, "%0.fms", system_time * 1000.0);
+		    break;
+		default:
+		    fprintf(stderr, "%%m");
+		    s--;
+		    break;
+		}
+		break;
+	    case 'u':
+		switch (*++s) {
+		case 'E':
+		    fprintf(stderr, "%0.fus", elapsed_time * 1000000.0);
+		    break;
+		case 'U':
+		    fprintf(stderr, "%0.fus", user_time * 1000000.0);
+		    break;
+		case 'S':
+		    fprintf(stderr, "%0.fus", system_time * 1000000.0);
+		    break;
+		default:
+		    fprintf(stderr, "%%u");
+		    s--;
+		    break;
+		}
+		break;
 	    case '*':
 		switch (*++s) {
 		case 'E':




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] Millisecond and microsecond specifiers for TIMEFMT
  2017-12-12  1:58 [RFC] Millisecond and microsecond specifiers for TIMEFMT dana
@ 2017-12-13 18:01 ` Peter Stephenson
  2017-12-14  2:49   ` dana
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2017-12-13 18:01 UTC (permalink / raw)
  To: zsh-workers

On Mon, 11 Dec 2017 19:58:41 -0600
dana <dana@dana.is> wrote:
> I am often interested in getting sub-second-precision timing on shell
> commands, but the `time` built-in doesn't offer this (not really,
> anyway). I can kind of work around it by writing a function that
> fiddles with SECONDS, but my function doesn't have the fancy syntax
> support that `time` has, which makes it inconvenient for measuring
> some things.

You don't need to fiddle with SECONDS beyond turning it into floating
point,

typeset -F SECONDS

at which point it's just a question of subtraction, which isn't usually
regarded as particularly problematic.

> What i'd really like is if TIMEFMT supported sequences for milliseconds and
> microseconds.
> 
> I had a look and it really does seem quite simple to implement. How would you
> feel about something like the change below?

Can't see any obvious problem with this.  Just needs documenting.

pws


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] Millisecond and microsecond specifiers for TIMEFMT
  2017-12-13 18:01 ` Peter Stephenson
@ 2017-12-14  2:49   ` dana
  2017-12-14 10:01     ` Teubel György
  0 siblings, 1 reply; 4+ messages in thread
From: dana @ 2017-12-14  2:49 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On 13 Dec 2017, at 12:01, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
>You don't need to fiddle with SECONDS beyond turning it into floating
>point...

Yeah, that's what i was referring to. It works OK, for what it is.

On 13 Dec 2017, at 12:01, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
>Can't see any obvious problem with this.  Just needs documenting.

Added test and documentation then. I have absolutely no idea how to get yodl
built on macOS, so if someone has time to double-check my syntax that'd be nice.

Thanks!

dana


diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index 5757111b2..bb5c8a54d 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -1616,10 +1616,12 @@ sitem(tt(%c))(Number of involuntary context switches.)
 sitem(tt(%J))(The name of this job.)
 endsitem()
 
-A star may be inserted between the percent sign and flags printing time.
-This cause the time to be printed in
+A star may be inserted between the percent sign and flags printing time
+(e.g., `tt(%*E)'); this causes the time to be printed in
 `var(hh)tt(:)var(mm)tt(:)var(ss)tt(.)var(ttt)'
 format (hours and minutes are only printed if they are not zero).
+Alternatively, `tt(m)' or `tt(u)' may be used (e.g., `tt(%mE)') to produce
+time output in milliseconds or microseconds, respectively.
 )
 vindex(TMOUT)
 item(tt(TMOUT))(
diff --git a/Src/jobs.c b/Src/jobs.c
index 226e7cff3..fc5bfacc6 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -728,6 +728,40 @@ printtime(struct timeval *real, child_times_t *ti, char *desc)
 	    case 'S':
 		fprintf(stderr, "%4.2fs", system_time);
 		break;
+	    case 'm':
+		switch (*++s) {
+		case 'E':
+		    fprintf(stderr, "%0.fms", elapsed_time * 1000.0);
+		    break;
+		case 'U':
+		    fprintf(stderr, "%0.fms", user_time * 1000.0);
+		    break;
+		case 'S':
+		    fprintf(stderr, "%0.fms", system_time * 1000.0);
+		    break;
+		default:
+		    fprintf(stderr, "%%m");
+		    s--;
+		    break;
+		}
+		break;
+	    case 'u':
+		switch (*++s) {
+		case 'E':
+		    fprintf(stderr, "%0.fus", elapsed_time * 1000000.0);
+		    break;
+		case 'U':
+		    fprintf(stderr, "%0.fus", user_time * 1000000.0);
+		    break;
+		case 'S':
+		    fprintf(stderr, "%0.fus", system_time * 1000000.0);
+		    break;
+		default:
+		    fprintf(stderr, "%%u");
+		    s--;
+		    break;
+		}
+		break;
 	    case '*':
 		switch (*++s) {
 		case 'E':
diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst
index 5e7d6acd8..217f7bea4 100644
--- a/Test/A01grammar.ztst
+++ b/Test/A01grammar.ztst
@@ -369,6 +369,10 @@
   (time cat) >&/dev/null
 0:`time' keyword (status only)
 
+  TIMEFMT='%E %mE %uE %* %m%mm %u%uu'; time (:)
+0:`time' keyword with custom TIMEFMT
+*?[0-9]##.[0-9](#c2)s [0-9]##ms [0-9]##us %\* %m%mm %u%uu
+
   if [[ -f foo && -d . && -n $ZTST_testdir ]]; then
     true
   else


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] Millisecond and microsecond specifiers for TIMEFMT
  2017-12-14  2:49   ` dana
@ 2017-12-14 10:01     ` Teubel György
  0 siblings, 0 replies; 4+ messages in thread
From: Teubel György @ 2017-12-14 10:01 UTC (permalink / raw)
  To: zsh-workers

On Wed, Dec 13, 2017 at 08:49:58PM -0600, dana wrote:
> On 13 Dec 2017, at 12:01, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> >You don't need to fiddle with SECONDS beyond turning it into floating
> >point...
> 
> Yeah, that's what i was referring to. It works OK, for what it is.
> 
> On 13 Dec 2017, at 12:01, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> >Can't see any obvious problem with this.  Just needs documenting.
> 
> Added test and documentation then. I have absolutely no idea how to get yodl
> built on macOS, so if someone has time to double-check my syntax that'd be nice.

OFF: You can install Yodl with MacPorts (https://www.macports.org/).


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-12-14 10:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-12  1:58 [RFC] Millisecond and microsecond specifiers for TIMEFMT dana
2017-12-13 18:01 ` Peter Stephenson
2017-12-14  2:49   ` dana
2017-12-14 10:01     ` Teubel György

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).