Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timing bash functions

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:

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?

Update: I'm want the time call 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.

like image 869
Alan W. Smith Avatar asked Aug 01 '12 15:08

Alan W. Smith


1 Answers

You can use command grouping rather than a subshell:

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...
    }
}
like image 122
jordanm Avatar answered Nov 28 '22 02:11

jordanm