定时 bash 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11762432/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timing bash functions
提问by Alan W. Smith
I'd like to see how long it takes for a bash function to run. After doing a little research, I've come up with this approach which uses a sub-shell:
我想看看 bash 函数运行需要多长时间。在做了一些研究之后,我想出了这种使用子外壳的方法:
function test-function() {
    time (
        rsync -av ~/tmp/test-dir-start/  ~/tmp/test-dir-end/
        rsync -av ~/tmp/test-dir-start/  ~/tmp/test-dir-end-2/
        # etc...
    )
}
Are there alternate/better ways to time bash functions?
是否有替代/更好的方法来计时 bash 功能?
Update: I'm want the timecall to be embedded in the function so that it runs every time without having to do time test-function. I should have been more clear on that.
更新:我希望将time调用嵌入到函数中,以便它每次都运行而无需执行time test-function. 我应该更清楚这一点。
回答by jordanm
You can use command grouping rather than a subshell:
您可以使用命令分组而不是子shell:
time { command1; command2; }
Update - more specific example:
更新 - 更具体的例子:
test-function() {
  time {
     rsync -av ~/tmp/test-dir-start/  ~/tmp/test-dir-end/
     rsync -av ~/tmp/test-dir-start/  ~/tmp/test-dir-end-2/
     # etc...
    }
}

