Why is repeat, while and for so slow to start up?

Viewed 112

I was making some code optimizations where it seems that the repeat, while and for constructs seem to have quite some overhead on startup. Here is an example:

microbenchmark::microbenchmark(
  {lapply(1:10, function(x) x)},
  {i <- 0; while (i < 10) i <- i + 1},
  {for (i in 1:10) i},
  {i <- 0; repeat if ((i <- i + 1) >= 10) break}
, times = 1000)
Unit: microseconds
                                            expr    min      lq     mean  median     uq      max neval
                 { lapply(1:10, function(x) x) }    7.4   12.80   15.101   14.30   15.5    919.9  1000
           { i <- 0; while (i < 10) i <- i + 1 } 1377.8 1431.85 1830.941 1475.10 1537.1  68344.9  1000
                           { for (i in 1:10) i }  838.0  880.00 1008.845  904.45  950.6  56744.9  1000
{ i <- 0; repeat if ((i <- i + 1) >= 10) break } 2092.4 2190.05 3265.421 2248.45 2343.2 467666.1  1000

If you use these constructs many times (e.g. within an outer loop), the 100x slower adds up considerably!

This seems contrary to my belief (and that of many others). Am I missing something?

Edit: since I didn't see any effect in my real code, made me wonder...

f <- function(x) { i <- 0; repeat if ((i <- i + 1) >= x) break }
microbenchmark::microbenchmark(
  f(10L)  
, times = 1000)
Unit: nanoseconds
   expr min  lq  mean median  uq   max neval
 f(10L) 700 800 898.9    900 900 10700  1000

Note the nanoseconds! These are the times I expect from these constructs (e.g. mainly the overhead due to <-, + and >=). So it seems to be a problem with microbenchmark?

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
0 Answers
Related