2012-08-01 31 views
3

我想看看bash函数运行需要多长时间。做了一些研究之后,我想出了这个方法,它使用一个子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... 
    ) 

} 

是否有替代/更好的方法来时间bash的功能?

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

回答

7

可以使用命令分组,而不是一个子shell:

time { command1; command2; } 

更新 - 更具体的例子:

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