Why is this insert fast in R?

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 lotsOfNumbers[1] <- 0 R code actually returns lotsOfNumbers to a new object, so this function actually scales with the length of lotsOfNumbers, not a constant.

I think that - this 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 enter both lines at the same time, speeding up the second line?

+5
source share

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


All Articles