Simplify $&time
and increase its flexibility
#210
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the
$&time
primitive and thetime
function. It fixes #154.Previously, the basic steps of
$&time
were: (1) measure real time and usage times; (2) run the given command; (3) measure real times and usage again; (4) take the difference and print the result.We now shrink the scope of
$&time
so that takes an optional previous set of times as arguments, and it (1) measures the real and usage times, (2) subtracts the argument-times if given, and (3) returns the result. Like this:The built-in
time
function is constructed from this new$&time
as:The first major benefit of this es-written
time
is that it is forkless:time {a = b}; echo $a
actually does the right thing now, becausetime
doesn't need to fork off a child process.The second major benefit of this es-written
time
is that it's flexible -- if you really want atime
that forks, you can simply do that (just make sure to do your timing measurements within the child process, or you'll add the overhead offork
to your timing!)But wait, there's more --- The reduced-scope
$&time
(and some code cleanup to go along with it) makes it easier to choose among a few different methods of gathering timing information, so we do so, and now we have significantly more precise real-time data on systems that support it.This version of
$&time
also makes custom time formatting possible. A motivated user can feed the raw microsecond output of$&time
toprintf
, orawk
, or whatever, to report the time the way they want. Or maybe just look at the real-time value and throw the usage values out. Or, one could use the timing data other ways...This
$&time
also enables new timing-related use cases. For one thing, a rawlet ((s _) = <=$&time) echo $s
approximates thetimes
command in other shells. For another, we can do more complex timing setups, likeWe can also do some, err, mild hackery to measure "has it been more than $X?" This can be used for things like
This could in theory be done with
date +%s
, but that would potentially add a lot of fork-exec overhead to gather and do math on all the timestamps. This way also works around es' lack of general arithmetic support, and supports quite a bit of precision.Limitations of this setup:
Across es invocations, results get wacky with all the measurements. Across processes (like
fork {}
) results get wacky with usage times specifically.Formatting of negative times is broken. I would like to fix this before merging.