I was always slightly crippled due to the lack of accuracy that I see in the table markings with system.time
and rbenchmark
(since time accuracy may be absent) and saw that Hadley refers to the microbenchmark
package, so I decided to give it a whirlwind, as shown below. I pitted mean
against f <- function(x) {sum(x)/length(x)}
and expected mean
to make the path better than f
, but the results, as I understand them, do not indicate that this is true.
- Do I really not understand the results?
- Is f faster than average?
- Is microbusiness still in beta, and does it need to be ironed outside the home?
I run R2.15 on a machine with a win of 7 (since microbenchmark performs timings differently depending on your OS).
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)
source share