How to run "time" on a function in zsh
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 How To Run &Quot;Time&Quot; On A Function In Zsh
00:19 Answer 1 Score 5
01:06 Accepted Answer Score 7
01:28 Thank you
--
Full question
https://superuser.com/questions/688128/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#bash #zsh #time
#avk47
ACCEPTED ANSWER
Score 7
@John1024 gave you the answer for bash
. I try to answer the zsh
tag...
You get the timing statistics, if you spawn a subshell for your function:
% zsh
% f() { sleep 1 }
% time f
% time (f)
( f; ) 0.00s user 0.05s system 4% cpu 1.061 total
% time sleep 1
sleep 1 0.00s user 0.03s system 2% cpu 1.045 total
This adds a little overhead, but as you can see from this (non-faked ;)
) example, it's probably insignificant.
ANSWER 2
Score 5
You have tagged this with both bash
and zsh
. This answer applies to bash
.
This is an error in bash
:
f() { ls -l }
What works instead is:
f() { ls -l ; }
With that new definition of f
, the time
command works.
Under bash
, when grouping commands together in braces, the last command must be followed by a semicolon or a newline. This is documented under "Compound Commands" in man bash
.
(I would test this solution under zsh
but I don't currently have it installed. But, as per this SO post, the solution under zsh
might be to run f
in a subshell. Update: See @mpy's answer for zsh
.)