zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zach Riggle <zachriggle@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Timing function execution
Date: Sat, 6 Nov 2021 09:15:45 -0700	[thread overview]
Message-ID: <CAH+w=7Z2jJqxSAa6sD_+jBEbDfHTfa_aMr_9WLfWG5T1OcCNFw@mail.gmail.com> (raw)
In-Reply-To: <CAMP9c5=YX9v969sVNcVcpbb8R6e4EoyrAMpqpEDAnpSYQi9Sbw@mail.gmail.com>

On Sat, Nov 6, 2021 at 5:11 AM Zach Riggle <zachriggle@gmail.com> 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.)


  reply	other threads:[~2021-11-06 16:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-06 12:11 Zach Riggle
2021-11-06 16:15 ` Bart Schaefer [this message]
2021-11-06 16:21   ` Roman Perepelitsa
2021-11-06 18:17     ` Bart Schaefer
2021-11-06 18:35       ` Bart Schaefer
2021-11-06 18:40         ` Bart Schaefer
2021-11-06 18:43           ` Roman Perepelitsa
2021-11-06 19:01             ` Bart Schaefer
2021-11-06 20:37               ` Zach Riggle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Z2jJqxSAa6sD_+jBEbDfHTfa_aMr_9WLfWG5T1OcCNFw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zachriggle@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).