adv-r
adv-r copied to clipboard
Update Perf-improve.Rmd - error in comparing the variations of computing the mean
Prose claims mean(x) is considerably slower than sum(x) / length(x). However, microbenchmark output shows that mean(x) is faster than sum(x) / length(x). Output included below.
mean1 <- function(x) mean(x) mean2 <- function(x) sum(x) / length(x)
x <- runif(1e5)
bench::mark(
mean1(x),
mean2(x)
)[c("expression", "min", "median", "itr/sec", "n_gc")]
#> # A tibble: 2 x 4
#> expression min median itr/sec
#> bch:expr bch:tm bch:tm
I assign the copyright of this contribution to Hadley Wickham.