# [Rcpp-devel] First foray into Rcpp and comparison of speed with R

Gregor GORJANC gregor.gorjanc at bf.uni-lj.si
Thu Sep 1 17:59:26 CEST 2011

```Hi,

On 1 September 2011 17:35, Dirk Eddelbuettel <edd at debian.org> wrote:

> ... I kind of hoped for more, but given that I vectorized much of R
> | code and that computations are simple additions and multiplications I
> wonder if
> | I could get much more gains with Rcpp foray.
>
> Hard to say in general terms.  Rcpp sugar can help, Romain has hidden some
> loop unrolling inside it and that helps in the benchmark example in the
> Rcpp-introduction vignette (and JSS article) but it doesn't imply all sugar
> code will be faster.
>

OK. But can we say that C++ for() loop via Rcpp can be speed up further with
Rcpp suger? Say simple sum of a vector or something simple like that. I do
not know, but I would think not. I am correct? I do agree that it would ease
the development and interpretability of code.

...
>  |   // --- Compute ---
> |
> |   for(i = 1; i < nI+1; i++) {
>
> Careful about i=1:  C/C++ indices run from 0 to n-1.
>

Thx for this, but it was deliberate as I rely on having zero in the "first"
row.

> |     for(t = 0; t < nT; t++) {
> |       pa(i, t) = c1 * ped(ped(i, 1), t+3) +
> |                  c2 * ped(ped(i, 2), t+3);
> |
> |       w(i, t) = ped(i, t+3) - pa(i, t);
>
> The recursive nature may make it tricky to vectorise this.
>

I thought so, but wanted to ask the experts.

...
> |   // --- Return ---
> |
> |   Rcpp::List ret;
> |   ret["pa"] = pa;
> |   ret["w"]  = w;
> |   ret["xa"] = xa;
> |   return(ret);
>
> We usually do
>
>  Rcpp::List ret = Rcpp::List::create(Rcpp::Named("pa", pa),
>                                      Rcpp::Named("w", w);
>                                      Rcpp::Named("xa", xa));
>  return ret;
>
> to avoid re-allocation.  But that is minuscule, and your code is arguably
> as easy or easier to read :)
>

Did I understand correctly that your proposal is to create return object all
in one spot to avoid changing it as I did three times?

Thx for replies and so neat way to use so powerfull tools in R!!!

gg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110901/b62ccc67/attachment.htm>
```