I am writing R code to create a square matrix. So my approach is:
According to this article we can do better than preallocating with NA by preallocating with NA_real_. From the article:
as soon as you assign a numeric value to any of the cells in 'x', the matrix will first have to be coerced to numeric when a new value is assigned. The originally allocated logical matrix was allocated in vain and just adds an unnecessary memory footprint and extra work for the garbage collector. Instead allocate it using NA_real_ (or NA_integer_ for integers)
As recommended: let's test it.
testfloat = function(mat){
n=nrow(mat)
for(i in 1:n){
mat[i,] = 1.2
}
}
>system.time(testfloat(matrix(data=NA,nrow=1e4,ncol=1e4)))
user system elapsed
3.08 0.24 3.32
> system.time(testfloat(matrix(data=NA_real_,nrow=1e4,ncol=1e4)))
user system elapsed
2.91 0.23 3.14
And for integers:
testint = function(mat){
n=nrow(mat)
for(i in 1:n){
mat[i,] = 3
}
}
> system.time(testint(matrix(data=NA,nrow=1e4,ncol=1e4)))
user system elapsed
2.96 0.29 3.31
> system.time(testint(matrix(data=NA_integer_,nrow=1e4,ncol=1e4)))
user system elapsed
2.92 0.35 3.28
The difference is small in my test cases, but it's there.