Suppose you want to benchmark the following piece of R fibonacci code
(1)
fibR <- function(n)
{
if(n == 0) return(0)
if(n == 1) return(1)
return (fibR(n - 1) + fibR(n - 2))
}
You can use the good ol' proc.time() function like this
(2)
a <- proc.time()
fibR(30)
proc.time() - a
Whose output will look something like this
(3)
user system elapsed
8.27 0.00 8.30
What this means is that the program took 8.27 user seconds to run the code and used 0 system seconds on resources in that time. Ideally you need to add the user and system times to get the full picture of how long your code took to run.
The elapsed time denotes the time it took for the day to go by between the first line of code in (2) to the 3rd line of code in (2). It means that it took 8.30 seconds from start to finish of the program in terms of time of day. It would show the total time it took for me to execute line 1 of (2) then go for a coffee, then execute line 2 of (2) and then take a piss and finally execute line 3 of (2). The total time for doing all this should reflect under elapsed. This metric baffles me though, because when I executed the code in (2) I ran it with a Ctrl-R shortcut over all 3 lines of the code. Doing this definitely could not have taken 8.3 seconds as I did not go for a coffee or take a piss in between. Seriously. Anyway...
There is another package called "rbenchmark" quoted in the book. Neat simple to use.
(4)replications stands for the number of times you want to repeat the function you want to benchmark. columns represent the columns you want to see in the output dataframe. Output looks like
benchmark(replications = 1, fibR(30), columns = c('test','elapsed'))
(5)
test elapsed
1 fibR(30) 8.23
Comments
Post a Comment