In running simulations/bootstraps/etc. in Mata I often find it helpful to work with matrixes that accumulate each replication's results row-by-row. I've always had an informal sense that it is faster to (a) work with an accumulation matrix defined up front as an empty matrix J(r,c,.) (r is the # of replications) and then populate its rows replication-by-replication than (b) set up a zero-row empty matrix J(0,c,.) and then row-append to it replication-by-replication.
How much faster is (a) than (b) I did not appreciate until running this little timing comparison. I was stunned.
Results:
How much faster is (a) than (b) I did not appreciate until running this little timing comparison. I was stunned.
Code:
mata
timer_clear()
rseed(2345)
v=J(1,5,1)
r=100000
timer_on(1)
ctch1=J(r,5,.)
for (j=1;j<=r;j++) {
ctch1[j,.]=v
}
timer_off(1)
timer_on(2)
ctch2=J(0,5,.)
for (j=1;j<=r;j++) {
ctch2=ctch2\v
}
timer_off(2)
ctch1==ctch2
timer()
end
Code:
: : ctch1==ctch2 1 : : timer() ----------------------------------------------------------------------------------------------------- timer report 1. .013 / 1 = .013 2. 59.3 / 1 = 59.268 -----------------------------------------------------------------------------------------------------

Comment