ZSH: How to time a block of code?

為{幸葍}努か 提交于 2019-11-30 04:42:56
Zsolt Botykai

Try the following instead:

{ time ( echo hello ; sleep 10s;  echo hola ; ) } 2>&1 
Pablo Castellazzi

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

Try replace { with ( ? I think this should help

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!