zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: zsh-workers@zsh.org
Subject: [RFC] Millisecond and microsecond specifiers for TIMEFMT
Date: Mon, 11 Dec 2017 19:58:41 -0600	[thread overview]
Message-ID: <6A9332CC-D501-4B1C-9347-92D0E87E45D7@dana.is> (raw)

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':




             reply	other threads:[~2017-12-12  1:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-12  1:58 dana [this message]
2017-12-13 18:01 ` Peter Stephenson
2017-12-14  2:49   ` dana
2017-12-14 10:01     ` Teubel György

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=6A9332CC-D501-4B1C-9347-92D0E87E45D7@dana.is \
    --to=dana@dana.is \
    --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).