I work primarily in MATLAB but I think the answer should not be too hard to carry over from one language to another.
In R, matrix multiplication is very optimized, i.e. is really just a call to BLAS/LAPACK. However, I'm surprised this very naive C++ code for matrix multiplication seems reliably 30% faster.
I'm currently forming a matrix from a vector in MATLAB following the scheme described below:Given is a vector x containing ones and zeros in an arbitrary order, e.g.