R loop becomes slower and slower

I am trying to understand why this bit of code (adapted from R Benchmark 2.5 ) becomes slower and slower (on average) as the number of iterations increases.

require(Matrix) c <- 0; for (i in 1:100) { a <- new("dgeMatrix", x = rnorm(3250 * 3250), Dim = as.integer(c(3250, 3250))) b <- as.double(1:3250) invisible(gc()) timing <- system.time({ c <- solve(crossprod(a), crossprod(a, b)) }) print(timing) rm(a, b, c) } 

Here is an example of a sample that is slightly different from one run to the next.

As I understand it, nothing should be saved from one iteration to another, but time slowly increases from 1 second in the first few cycles to more than 4 seconds in subsequent cycles. Do you have any ideas what causes this, and how can I fix it?

Switching the for loop into the application * seems to give similar results.

I know that the code is not optimized, but it comes from a widely used test, and depending on what causes this behavior, it may indicate a serious bias in its results (which by default only runs three times).

I am running R version 3.0.1 (x86_64) on Mac OS 10.8.4 with 16 GB of RAM (many of them are free). BLAS is OpenBLAS.

+8
performance benchmarking r blas
source share
2 answers

One solution would be to use a compiler package to compile the code into byte code. This should fix odd-time problems, as it will call the same compiled code at each iteration. This should also make your code faster. To enable the compiler in your code, include the two lines below:

 library(compiler) enableJIT(3) 

If compiling the code does not fix the problem, then the set of suspicious problems will narrow.

+1
source share

Perhaps you could try to make the code inside a for loop in a function. Thus, in fact, no one can influence the other. In addition, it eliminates the clutter caused by the excessive needs of rm () and gc ().

 require(Matrix) NewFun <- function() { a <- new("dgeMatrix", x = rnorm(3250 * 3250), Dim = as.integer(c(3250, 3250))) b <- as.double(1:3250) timing <- system.time({ c <- solve(crossprod(a), crossprod(a, b)) }) print(timing) } for (i in 1:100) { NewFun() } 
0
source share

All Articles