[Rcpp-devel] RcppArmadillo: resize vs. insert_cols

Ramon Diaz-Uriarte rdiaz02 at gmail.com
Mon Dec 10 16:09:36 CET 2012


Hi Conrad,

Thanks a lot. Understood; I'll stay away from premature optimization,
then, and see if it is really needed.


Best,

R.


On Mon, 10 Dec 2012 23:48:36 +1000,c s <conradsand.arma at gmail.com> wrote:
> Hi Ramon,

> Option (b) would be more efficient, at the expense of slightly more code.

> However, you can use option (a) to quickly get something working. Once you
> test that your code works okay, you can always optimise it by selectively
> refactoring it into (b).

> If the code in question is not inside a critical loop, you may not need to
> bother with the refactoring. Eg. if most of the time is spent inside matrix
> multiplication, or solve(), or svd(), etc, refactoring would make little
> overall difference.



> On Monday, December 10, 2012, Ramon Diaz-Uriarte <rdiaz02 at gmail.com> wrote:
> >
> > Dear All,
> >
> > I am using RcppArmadillo, and I am creating matrices in the C++ code
> > (i.e., these are not matrices passed from R). The sizes of these matrices
> > might need to increase dynamically during run time, with elements being
> > added at the end. What is the recommended way of doing this:
> >
> >
> > a) Just insert elements as needed. E.g.: Y.insert_cols(Y.n_cols,
> the_new_thing);
> >
> > b) Create initial object. Double size of object (using resize) each time I
> > run out of space. Copy the_new_thing to the appropriate place.
> >
> >
> >
> > I thought b) was a reasonable manual approach (e.g., Skiena, "The
> > algorithm design manual, 2nd ed", p. 67), but I think I've read that for
> > std::vector, a) (with push_back), is a better choice than b) (using
> > reserve, not resize).
> >
> >
> > For me, a) involves writing less code.
> >
> > Thanks,
> >
> >
> > R.
> >
> >
> > --
> > Ramon Diaz-Uriarte
> > Department of Biochemistry, Lab B-25
> > Facultad de Medicina
> > Universidad Autónoma de Madrid
> > Arzobispo Morcillo, 4
> > 28029 Madrid
> > Spain
> >
> > Phone: +34-91-497-2412
> >
> > Email: rdiaz02 at gmail.com
> >        ramon.diaz at iib.uam.es
> >
> > http://ligarto.org/rdiaz
> >
> > _______________________________________________
> > Rcpp-devel mailing list
> > Rcpp-devel at lists.r-forge.r-project.org
> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Ramon Diaz-Uriarte
Department of Biochemistry, Lab B-25
Facultad de Medicina 
Universidad Autónoma de Madrid 
Arzobispo Morcillo, 4
28029 Madrid
Spain

Phone: +34-91-497-2412

Email: rdiaz02 at gmail.com
       ramon.diaz at iib.uam.es

http://ligarto.org/rdiaz



More information about the Rcpp-devel mailing list