Consider the following code:
lotsOfNumbers <- rep(pi, 5 * 10^7)
system.time(lotsOfNumbers[1] <- 0)
If I enter one line of code at a time, the second line of code takes about 0.267 seconds to evaluate on my computer. This is not surprising to me: I know that the R code lotsOfNumbers[1] <- 0
actually recopies lotsOfNumbers
into a new object, so this function actually scales with the length of lotsOfNumbers rather than being constant.
What is surprising to me is that if you enter both lines of code at the same time, system.time
will report that the second line of code takes 0.000 seconds on my computer.
Why would entering both lines at the same time speed up the second line?