It seems that zsh
does not support to time builtin commands.
Try to use a shell that includes support for time
as a reserved word (this applies to zsh
) and also supports to time builtin commands (this does not apply to zsh
).
BTW: this works for ksh88
, ksh93
, bosh
(the current Bourne Shell), mksh
and bash
.
BTW: The correct way to do timing is to take the timing for the shell and all it's sub procecces (that may be called programs) and to print the difference from before and after the command. If the timing includes the time spent in the shell itself, this allows to time builtin commands.
The background is that the timing that works inside the shell should be compatible to what the external time
command does and the external time
command prints the sum of all times of all processes created for a specific command. If time
did not include the values for sub processes, it would e.g. print too low values for the command cc
as most of the work is done inside sub processes of cc
.
time (source ...)
ortime (echo x)
would work. Other shells may end up including the times of other processes when timing builtins or functions. See for instancebosh -c 'seq 10000000 > /dev/null & time read'
that includes the user/sys time ofseq
if you press enter afterseq
has returned. IIRC mksh has some improvements on that front. – Stéphane Chazelas Oct 28 '18 at 08:59zsh
includes sub-processes when timing functions which you have to time withtime (myfunction)
.zsh
is right for all the times it reports. – Stéphane Chazelas Oct 28 '18 at 12:25/usr/bin/time /bin/echo $(find / | wc -l)
report something different fromtime /bin/echo $(find / | wc -l)
in all shells that have atime
keyword exceptzsh
. – Stéphane Chazelas Oct 28 '18 at 12:27bash
for things liketime { cmd >(cmd2); }
which generally fails to accountcmd2
's time (eventime (cmd >(cmd2))
fails,zsh
is OK). – Stéphane Chazelas Oct 28 '18 at 12:31bosh
asbosh
does not implement aSIGCHLD
handler and asbosh
does not yet implement a list of recently finished job status data.bosh
implements a similar method ascsh
and in all but a few corner cases reports better timing that other POSIX like shells do. With regards to your previous example:/usr/bin/time
cannot see the times from the command substitution, butksh88
already defines that the whole command is to be timed with thekeyword time
, sozsh
is wrong. – schily Oct 28 '18 at 12:58cmd1
andcmd2
incmd1 | cmd2
. ksh doesn't do it, that doesn't mean it's wrong as it's not ksh's intention to reproduce the zsh behaviour. – Stéphane Chazelas Oct 28 '18 at 13:31time
in the shell if you don't get any advantages from that fact? – schily Oct 28 '18 at 13:49time foo | bar
is much more useful inzsh
than with the non-builtintime
(or thetime
of other shells), it's also got it'sTIMEFMT
format à la csh, and you can time functions and builtins or compound commands (as long as you remember to put them in subshells, which I'll grant you, is somewhat of a limitation compared to ksh/bash but at least means it's reliable) which you can't do with an externaltime
. – Stéphane Chazelas Oct 28 '18 at 16:39TIMEFORMAT
inbosh
are more useful than what you get withzsh
in special asbosh
supports up to microsecond granularity for the times andzsh
usually just prints0.00
for the timing of e.g.ls -l
. I recommend you to try out theTIMEFORMAT
sample value from thebosh
man page. – schily Oct 28 '18 at 17:31%*U
for instance on zsh to get millisecond precision, but getting that much precision (anything smaller than a scheduler typical time slice) doesn't make that much sense. If you want to time an operation with some level of precision, you'll be better off doing things liketime (repeat 10000 that-operation)
and take the average IMO. For elapsed time, you can also usetypeset -F SECONDS
like in ksh93 or use$EPOCHREALTIME
to get nanoseconds, but again, that much precision doesn't make that much sense in shells that deal with commands having to be run in separate processes. – Stéphane Chazelas Oct 28 '18 at 18:11bosh
gives you what you need for you need for performance mesurement. – schily Oct 28 '18 at 18:35bosh -c 'TIMEFORMAT=%6U; time ls'
on a Solaris 11.4 VM here, I get0.001383
on the first run,0.000907
on the second,0.001173
on the 3rd. It seems 1 ms is enough precision./usr/bin/time bosh -c 'TIMEFORMAT=%6U; c=0; time while [ "$c" -lt 1000 ]; do ls; c=$(($c+1)); done'
has bosh report0.836859
user time while/usr/bin/time
reports0.0
user time. Might be something wrong with /usr/bin/time there. It also looks as if the zsh build on that machine doesn't account time of children processes. – Stéphane Chazelas Oct 28 '18 at 19:57ptime
you get that resolution, but note there are variations from many sources e,g, interrupt scheduling and the time spent in the scheduler is not always the same. In multi user mode and with more CPUs, there is another imprecision... You get less variation if you write a CPU bound application and lock it to a specific CPU. You believe the results depend on the clock interrupt rate from the scheduler, so expect a variation of 10ms. I however only see a variation of approx. 100usec. It is better than what I get from Linux. – schily Oct 28 '18 at 22:56