Sometimes we would like to time the speed of some things for comparisons,
but those things execute too fast to measure. So we arrange to put them
inside loops, and then measure the loops. That is not very practical, because
the loops in bash add too much overhead for the things we're trying to measure.
It would be much better if command time is modified to take an option -n N,
N being how many times to execute the thing that is getting timed. That would
be much more efficient, and the measurement would be much more precise.
I looked into that, and it seems like adding a new option to the time command
would require changes to the source code that would be way too messy. So, the
next best thing is controlling the time command behavior through a variable.
$ TIMEREPEAT=3; time { ((x++)); echo $x;}
$ TIMEREPEAT=100000; time true
Patch (apply with `patch -p0'):
Patch (apply with `patch -p0'):
--- builtins/reserved.def
+++ ../bash-5.3-patched/builtins/reserved.def Mon Sep 22 20:35:06 2025
@@ -91,6 +91,8 @@
-p print the timing summary in the portable Posix format
The value of the TIMEFORMAT variable is used as the output format.
+The value of the TIMEREPEAT variable is used to indicate the number
+of times the pipline will be ran before the total time is measured.
Exit Status:
The return status is the return status of PIPELINE.
--- execute_cmd.c
+++ ../bash-5.3-patched/execute_cmd.c Mon Sep 22 20:38:17 2025
@@ -1486,8 +1486,13 @@
COPY_PROCENV (top_level, save_top_level);
command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX);
code = setjmp_nosigs (top_level);
- if (code == NOT_JUMPED)
- rv = execute_command_internal (command, asynchronous, pipe_in, pipe_out,
fds_to_close);
+ if (code == NOT_JUMPED) {
+ intmax_t nrepeat;
+ char *v = get_string_value ("TIMEREPEAT");
+ if (v == 0 || *v == 0 || valid_number (v, &nrepeat) == 0 || nrepeat < 1)
nrepeat = 1;
+ for(int i = 0; i < nrepeat; i++)
+ rv = execute_command_internal (command, asynchronous, pipe_in, pipe_out,
fds_to_close);
+ }
COPY_PROCENV (save_top_level, top_level);
if (code == NOT_JUMPED)
--
2.51.0