Unexpected results: microbenchmark

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) 
+6
source share
2 answers

I could be wrong, but this does not seem surprising to me. Before mean.default can call .Internal(mean(x)) , it must check the 3 if , calculate the length of x , and then check another if . And the time difference is quite small.

Calling .Internal(mean(x) directly is a bit faster:

 library(microbenchmark) x <- 1:10000 f1 <- function(x) {sum(x)/length(x)} f2 <- function(x) {.Internal(mean(x))} res <- microbenchmark( mean(x), f1(x), f2(x), times=1000L) print(res) Unit: microseconds expr min lq median uq max 1 f1(x) 32.195 32.4605 32.8850 33.4645 106.997 2 f2(x) 21.840 22.0580 22.2015 22.6270 55.316 3 mean(x) 35.393 35.9840 36.1860 36.4420 91.203 
+8
source

I think you will find that if you increase the size of X by 10 times, you will see more consistent results. Honestly, I would be surprised if you really could get microsecond time accuracy on a computer with a multitasking operating system.

You may also consider:

  • Do you work on a laptop or a machine with automatic scaling of the processor frequency?
  • Warm up?
  • Binding your process to one core.
+2
source

Source: https://habr.com/ru/post/914419/


All Articles