adv-r icon indicating copy to clipboard operation
adv-r copied to clipboard

Update Perf-improve.Rmd - error in comparing the variations of computing the mean

Open shilaan opened this issue 4 years ago • 0 comments

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 #> 1 mean1(x) 162µs 176µs 5571. #> 2 mean2(x) 190µs 202µs 4914.

I assign the copyright of this contribution to Hadley Wickham.

shilaan avatar May 11 '21 16:05 shilaan