Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Timing function execution
- X-seq: zsh-users 27311
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zach Riggle <zachriggle@xxxxxxxxx>
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: Timing function execution
- Date: Sat, 6 Nov 2021 09:15:45 -0700
- Archived-at: <https://zsh.org/users/27311>
- In-reply-to: <CAMP9c5=YX9v969sVNcVcpbb8R6e4EoyrAMpqpEDAnpSYQi9Sbw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <CAMP9c5=YX9v969sVNcVcpbb8R6e4EoyrAMpqpEDAnpSYQi9Sbw@mail.gmail.com>
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