I've always been bugged a bit by the lack of accuracy I see bench marking with system.time
and rbenchmark
(in that the precision of the timing may be lacking) and saw Hadley reference the microbenchmark
package recently. So I decided to give it a whirl as seen below. I pitted mean
against f <- function(x) {sum(x)/length(x)}
and expected mean
to do way better than f
but the results, as I understand them, do not indicate this to be true.
- Am I misunderstanding the results?
- Is f actually faster than mean?
- Is microbenchmark still in a beta phase and this needs to be ironed out?
I'm running R2.15 on a win 7 machine (as microbenchmark does timings differently depending on your OS).
The Results
Unit: microseconds
expr min lq median uq max
1 f(x) 19.130 20.529 20.529 20.996 286.00
2 mean(x) 28.927 29.860 30.327 30.327 672.31
The Code
library(microbenchmark)
x <- 1:10000
f <- function(x) {sum(x)/length(x)}
mean(x)
res <- microbenchmark(
mean(x),
f(x),
times=1000L)
print(res)
boxplot(res)
microbenchmark
. If you're doing more than one or two results, plotting can help greatly but the default output is a little on the ugly side. I wrote an autoplot function for ggplot2 which may show up in one of these releases (check github in the meantime). Examples: https://mcmap.net/q/1164748/-running-functions-in-r-multiple-times-during-benchmarking – Gorillaf
were lower and a scatterplot indicated this as well. joran nailed this one. – Motorcycle