<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Serguei: Since you were kind enough to time things in my previous question, I just wanted to follow up with something</div><div>I found interesting. I take your code below and only change it in that use a NumericVector instead of an std::double<vector> <br></div><div>and call it mybar3. Somewhat surprisingly, the timing improved considerably, even beating c() in R. I only recently learned that <br></div><div>NumericVector could use begin() and end() statements so I'm not the person to understand why except that, from another thread,</div><div>I learned that, to convert to std:: double, deep copies need to be made. So, maybe that's why ? I'm going to look at Jan's <br></div><div>solution next and, if I find anything interesting there, will send it.</div><div><br></div><div dir="ltr">#===================================================================================================<br></div><div dir="ltr"><br></div><div dir="ltr">#include <Rcpp.h><br>using namespace Rcpp;<br><br>// [[Rcpp::export]]<br>std::vector<double> mybar(const std::vector<double>& x, double firstelem) {<br>   std::vector<double> tmp(x.size() + 1);<br>   tmp[0] = firstelem;<br>   for (int i = 1; i < (x.size()+1); i++)<br>     tmp[i] = x[i-1];<br>   return tmp;<br>}<br>// [[Rcpp::export]]<br>std::vector<double> mybar2(const std::vector<double>& x, double firstelem) {<br>   std::vector<double> tmp(x.size() + 1);<br>   tmp[0] = firstelem;<br>   std::copy(x.begin(), x.end(), tmp.begin()+1);<br>   return tmp;<br>}<br><br>// [[Rcpp::export]]<br>NumericVector mybar3(NumericVector x, double firstelem) {<br>   NumericVector tmp(x.size() + 1);<br>   tmp[0] = firstelem;<br>   std::copy(x.begin(), x.end(), tmp.begin()+1);<br>   return tmp;<br>}<br><br>/*** R<br><br>library(microbenchmark)<br>n=10000<br>testvec = c(1,seq_len(n))<br>testelem <- 7<br><br>temp2 <- mybar2(testvec, testelem)<br><br>microbenchmark(c(testelem, testvec), mybar(testvec,testelem), mybar2(testvec, testelem), mybar3(testvec, testelem))<br><br>*/</div><div dir="ltr"><br></div><div dir="ltr">  microbenchmark(c(testelem, testvec), mybar(testvec,testelem), mybar2(testvec, testelem), mybar3(testvec, testelem))<br>Unit: microseconds<br>                      expr    min      lq     mean  median      uq     max neval<br>      c(testelem, testvec) 14.780 30.9765 33.42152 32.5605 33.8890  74.750   100<br>  mybar(testvec, testelem) 32.570 35.6150 47.95297 37.9895 42.5220 705.267   100<br> mybar2(testvec, testelem) 28.520 31.7695 45.36286 33.8250 37.7335 813.504   100<br> mybar3(testvec, testelem)  9.449 25.0345 33.21592 26.2625 27.4070 635.800   100<br> <br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Dec 10, 2018 at 6:10 AM Serguei Sokol <<a href="mailto:serguei.sokol@gmail.com">serguei.sokol@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Le 09/12/2018 à 09:35, Mark Leeds a écrit :<br>
> Hi All: I wrote below and it works but I have a strong feeling there's a <br>
> better way to do it.<br>
If performance is an issue, you can save few percents of cpu time by <br>
using std::copy() instead of explicit for loop. Yet, for this operation <br>
R's c() remains the best bet. It is more then twice faster than both <br>
Rcpp versions below:<br>
<br>
#include <Rcpp.h><br>
using namespace Rcpp;<br>
<br>
// [[Rcpp::export]]<br>
std::vector<double> mybar(const std::vector<double>& x, double firstelem) {<br>
   std::vector<double> tmp(x.size() + 1);<br>
   tmp[0] = firstelem;<br>
   for (int i = 1; i < (x.size()+1); i++)<br>
     tmp[i] = x[i-1];<br>
   return tmp;<br>
}<br>
// [[Rcpp::export]]<br>
std::vector<double> mybar2(const std::vector<double>& x, double firstelem) {<br>
   std::vector<double> tmp(x.size() + 1);<br>
   tmp[0] = firstelem;<br>
   std::copy(x.begin(), x.end(), tmp.begin()+1);<br>
   return tmp;<br>
}<br>
<br>
/*** R<br>
library(microbenchmark)<br>
n=100000<br>
testvec = c(1,seq_len(n))<br>
testelem <- 7<br>
microbenchmark(c(testelem, testvec), mybar(testvec,testelem), <br>
mybar2(testvec,testelem))<br>
*/<br>
<br>
# Ouput<br>
Unit: microseconds<br>
                       expr     min       lq      mean   median        uq<br>
       c(testelem, testvec) 247.098 248.5655  444.8657 257.3300  630.7725<br>
   mybar(testvec, testelem) 594.978 622.3560 1226.5683 637.0230 1386.8385<br>
  mybar2(testvec, testelem) 576.191 604.7565 1029.2124 616.1055 1351.6740<br>
        max neval<br>
   7587.977   100<br>
  22149.605   100<br>
  11651.831   100<br>
<br>
<br>
Best,<br>
Serguei.<br>
<br>
> I looked on the net and found some material from <br>
> back in ~2014 about concatenating<br>
> vectors but I didn't see anything final about it. Thanks for any insights.<br>
> <br>
> Also, the documentation for Rcpp is beyond incredible (thanks to dirk, <br>
> romain, kevin and all the other people I'm leaving out )  but is there a <br>
> general methodology for finding equivalents of R functions. For example, <br>
> if I want a cumsum function in Rcpp, how do I know whether to use the <br>
> stl with accumulate or if there's already one built in so<br>
> that I just call cumsum.<br>
> <br>
> Thanks.<br>
> <br>
> #=======================================================<br>
> <br>
> #include <Rcpp.h><br>
> using namespace Rcpp;<br>
> <br>
> // [[Rcpp::export]]<br>
> std::vector<double> mybar(const std::vector<double>& x, double firstelem) {<br>
>    std::vector<double> tmp(x.size() + 1);<br>
>    tmp[0] = firstelem;<br>
>    for (int i = 1; i < (x.size()+1); i++)<br>
>      tmp[i] = x[i-1];<br>
>    return tmp;<br>
> }<br>
> <br>
> /*** R<br>
> <br>
> testvec = c(1,2,3)<br>
> testelem <- 7<br>
> mybar(testvec,testelem)<br>
> <br>
> */<br>
> <br>
> #===============================<br>
> # OUTPUT FROM RUNNING ABOVE<br>
> #=================================<br>
>  > testvec <-  c(1,2,3)<br>
>  > testelem <- 7<br>
>  > mybar(testvec,testelem)<br>
> [1] 7 1 2 3<br>
>  ><br>
> <br>
> <br>
> <br>
> <br>
> <br>
> <br>
> <br>
> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
> <br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div>