计时bash功能

Ala*_*ith 6 bash time bash-function

我想看看bash函数运行需要多长时间.在做了一点研究之后,我想出了一种使用子shell的方法:

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...
    )

}
Run Code Online (Sandbox Code Playgroud)

是否有其他/更好的方法来计时bash功能?

更新:我希望将time调用嵌入到函数中,以便每次都可以运行而无需执行time test-function.我应该更清楚这一点.

jor*_*anm 10

您可以使用命令分组而不是子shell:

time { command1; command2; }
Run Code Online (Sandbox Code Playgroud)

更新 - 更具体的示例:

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...
    }
}
Run Code Online (Sandbox Code Playgroud)