ZSH: How to time a block of code?

后端 未结 4 579
死守一世寂寞
死守一世寂寞 2020-12-29 04:06

In bash I am able to write a script that contains something like this:

{ time {

#series of commands
echo \"something\"
echo \"another command\"
echo \"blah          


        
相关标签:
4条回答
  • 2020-12-29 04:44

    If you want to profile your code you have a few alternatives:

    • Time subshell execution like:

      time ( commands ... )

    • Use REPORTTIME to check for slow commands:

      export REPORTTIME=3 # display commands with execution time >= 3 seconds

    • setop xtrace as explained here

    • The zprof module

    0 讨论(0)
  • 2020-12-29 04:52

    You can also use the times POSIX shell builtin in conjunction with functions. It will report the user and system time used by the shell and its children. See http://pubs.opengroup.org/onlinepubs/009695399/utilities/times.html

    Example:

    somefunc() {
        code you want to time here
        times
    }
    

    The reason for using a shell function is that it creates a new shell context, at the start of which times is all zeros (try it). Otherwise the result contains the contribution of the current shell as well. If that is what you want, forget about the function and put times last in your script.

    0 讨论(0)
  • 2020-12-29 05:02

    Try the following instead:

    { time ( echo hello ; sleep 10s;  echo hola ; ) } 2>&1 
    
    0 讨论(0)
  • 2020-12-29 05:08

    Try replace { with ( ? I think this should help

    0 讨论(0)
提交回复
热议问题