Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Timing function execution



On Sat, Nov 6, 2021 at 5:11 AM Zach Riggle <zachriggle@xxxxxxxxx> wrote:
>
> What is the appropriate way to benchmark / log the execution time of a function?

"time" relies upon being able to use the OS-level profiling operations
on an external process.  Shell functions run in the current shell, so
there's no process to profile.  Consequently, in order to use "time"
you have to force the function to run in a subshell:

% time (foo)
hello
( foo; )  0.00s user 0.00s system 0% cpu 1.005 total

If all you need is the elapsed time, you can use $SECONDS.

% float SECONDS
% SECONDS=0; foo; print $SECONDS
hello
1.004018000e+00

(Saved almost 0.001 by not forking, I guess.)




Messages sorted by: Reverse Date, Date, Thread, Author