2012-08-01 78 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... 
    } 
}