<font class="Apple-style-span" face="'courier new', monospace">Hi,<br><br></font><div class="gmail_quote"><font class="Apple-style-span" face="'courier new', monospace">On 1 September 2011 17:35, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org">edd@debian.org</a>></span> wrote:<br>
</font><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><font class="Apple-style-span" face="'courier new', monospace">... I kind of hoped for more, but given that I vectorized much of R<br>
| code and that computations are simple additions and multiplications I wonder if<br>
| I could get much more gains with Rcpp foray.<br>
<br>
</font></div><font class="Apple-style-span" face="'courier new', monospace">Hard to say in general terms. Rcpp sugar can help, Romain has hidden some<br>
loop unrolling inside it and that helps in the benchmark example in the<br>
Rcpp-introduction vignette (and JSS article) but it doesn't imply all sugar<br>
code will be faster.<br></font></blockquote><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">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.</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><font class="Apple-style-span" face="'courier new', monospace">...</font></div>
<div class="im"><font class="Apple-style-span" face="'courier new', monospace">
| // --- Compute ---<br>
| <br>
| for(i = 1; i < nI+1; i++) {<br>
<br>
</font></div><font class="Apple-style-span" face="'courier new', monospace">Careful about i=1: C/C++ indices run from 0 to n-1.<br></font></blockquote><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">Thx for this, but it was deliberate as I rely on having zero in the "first" row.</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> </font></div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><font class="Apple-style-span" face="'courier new', monospace">
| for(t = 0; t < nT; t++) {<br>
| pa(i, t) = c1 * ped(ped(i, 1), t+3) +<br>
| c2 * ped(ped(i, 2), t+3);<br>
| <br>
| w(i, t) = ped(i, t+3) - pa(i, t);<br>
<br>
</font></div><font class="Apple-style-span" face="'courier new', monospace">The recursive nature may make it tricky to vectorise this.<br></font></blockquote><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">I thought so, but wanted to ask the experts.</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><font class="Apple-style-span" face="'courier new', monospace">...<br>
| // --- Return ---<br>
| <br>
| Rcpp::List ret;<br>
| ret["pa"] = pa;<br>
| ret["w"] = w;<br>
| ret["xa"] = xa;<br>
| return(ret);<br>
<br>
</font></div><font class="Apple-style-span" face="'courier new', monospace">We usually do<br>
<br>
Rcpp::List ret = Rcpp::List::create(Rcpp::Named("pa", pa),<br>
Rcpp::Named("w", w);<br>
Rcpp::Named("xa", xa));<br>
return ret;<br>
<br>
to avoid re-allocation. But that is minuscule, and your code is arguably<br>
as easy or easier to read :)<br></font></blockquote><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">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?</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">Thx for replies and so neat way to use so powerfull tools in R!!!</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">gg </font></div></div>