I can use time to get running time statistics:
% time zsh --version
zsh 5.6.2 (x86_64-pc-linux-gnu)
zsh --version 0.00s user 0.00s system 86% cpu 0.005 total
How can I time how long a source script takes to run?
% time source $NVM_DIR/nvm.sh
%
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 ofseqif you press enter afterseqhas returned. IIRC mksh has some improvements on that front. – Stéphane Chazelas Oct 28 '18 at 08:59zshincludes sub-processes when timing functions which you have to time withtime (myfunction).zshis 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 atimekeyword exceptzsh. – Stéphane Chazelas Oct 28 '18 at 12:27bashfor things liketime { cmd >(cmd2); }which generally fails to accountcmd2's time (eventime (cmd >(cmd2))fails,zshis OK). – Stéphane Chazelas Oct 28 '18 at 12:31boshasboshdoes not implement aSIGCHLDhandler and asboshdoes not yet implement a list of recently finished job status data.boshimplements a similar method ascshand in all but a few corner cases reports better timing that other POSIX like shells do. With regards to your previous example:/usr/bin/timecannot see the times from the command substitution, butksh88already defines that the whole command is to be timed with thekeyword time, sozshis wrong. – schily Oct 28 '18 at 12:58cmd1andcmd2incmd1 | 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:31timein the shell if you don't get any advantages from that fact? – schily Oct 28 '18 at 13:49time foo | baris much more useful inzshthan with the non-builtintime(or thetimeof other shells), it's also got it'sTIMEFMTformat à 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:39TIMEFORMATinboshare more useful than what you get withzshin special asboshsupports up to microsecond granularity for the times andzshusually just prints0.00for the timing of e.g.ls -l. I recommend you to try out theTIMEFORMATsample value from theboshman page. – schily Oct 28 '18 at 17:31%*Ufor 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 SECONDSlike in ksh93 or use$EPOCHREALTIMEto 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:11boshgives 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.001383on the first run,0.000907on the second,0.001173on 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.836859user time while/usr/bin/timereports0.0user 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:57ptimeyou 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