<div dir="ltr"><div>Hello Dear </div><div><br></div><div>Your suggestions are quite useful. I have deleted two for loops from my code and trying to use vectors in place of them. It is hard for me to produce a minimal example. However, now I am trying to use vectorization. I am having some error. Following is an example code. My basic purpose is subcube element wise multiplication with matrix's row. The operand % is not working in this case. Any suggestion would be appreciated.</div><div><br></div><div>Here a is a cube of dimension (3, 3, 1) and b is a matrix of dimension (3, 3).</div><div><br></div><div>#include <RcppArmadillo.h><br>using namespace Rcpp;<br>using namespace RcppArmadillo;<br>using namespace arma;<br>//[[Rcpp::depends(RcppArmadillo)]]<br>//[[Rcpp::export]]</div><div><br></div><div>arma::cube sub(arma::cube a, arma::mat b)<br>{<br>        a.subcube(2,0,0,2,2,0) = a.subcube(1, 0, 0, 1, 2, 0) .* b.row(0);<br>        return a;<br>}<br></div><div><br></div><div><br></div><div>The error is </div><div><br></div><div><br></div><div>subcube.cpp: In function ‘arma::cube sub(arma::cube, arma::mat)’:<br>subcube.cpp:11:65: error: ‘arma::Mat<eT>::row(arma::uword) [with eT = double; arma::uword = unsigned int](0u)’ cannot be used as a member pointer, since it is of type  arma::subview_row<double>’<br>  a.subcube(2,0,0,2,2,0) = a.subcube(1, 0, 0, 1, 2, 0) .* b.row(0);</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>Thank You so much again for suggesting how to think about to produce a speedy code.</div><div><br></div><div>Shaami<br><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 6, 2017 at 5:03 AM, Martyn Plummer <span dir="ltr"><<a href="mailto:plummerm@iarc.fr" target="_blank">plummerm@iarc.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Thu, 2017-01-05 at 05:39 -0700, Christian Gunning wrote:<br>
> ><br>
> > I am new to Rcpp. I have converted my R program into an Rcpp program.<br>
> > I want to ask a general question. Using Rcpp, my code is taking about 3<br>
> > minutes to produce output for 100 observations. But it is taking about 2.45<br>
> > hours to produce output for 1000 observations. Any suggestions or technical<br>
> > hint for this type of problem.<br>
><br>
> First off, please include a minimal working example with your<br>
> question. Without more information, there's not much we can do to<br>
> help.<br>
<br>
</span>I agree with Christian that a reproducible working example is necessary<br>
for detailed help with your problem. But I'm also bold enough to make a<br>
guess about what is going on.<br>
<br>
Your program is implementing an algorithm that is not linear in the<br>
sample size but polynomial. You can verify this by recording run times<br>
for sample sizes of 50, 60, 70, 80, 90, and 100. Plotting the run time<br>
against the sample size should show the non-linear relationship. At a<br>
sample size of 1000 the non-linear effects dominate, giving you a<br>
roughly 5-fold increase in the run time over what you expect from your<br>
smaller example.<br>
<br>
It was pointed out many years ago by Joel Spolsky that this phenomenon<br>
can arise from using a high level interface without understanding the<br>
low-level implementation details. See his Wikipedia entry:<br>
<br>
<a href="https://en.wikipedia.org/wiki/Joel_Spolsky#Schlemiel_the_Painter.27s_al" target="_blank" rel="noreferrer">https://en.wikipedia.org/wiki/<wbr>Joel_Spolsky#Schlemiel_the_<wbr>Painter.27s_al</a><br>
gorithm<br>
<br>
Frequent reallocations of existing memory are a typical cause of this<br>
problem.<br>
<br>
All of this means that finding the underlying problem might be quite<br>
hard for you to find, which is all the more reason to strip down your<br>
program to find the smallest example that shows this behaviour.<br>
<br>
Martyn<br>
<span><br>
> In the meantime, you might find this to be an interesting and helpful<br>
> read:<br>
> <a href="http://www.burns-stat.com/documents/books/the-r-inferno/" target="_blank" rel="noreferrer">http://www.burns-stat.com/<wbr>documents/books/the-r-inferno/</a><br>
><br>
> Best,<br>
</span>> Christian---------------------<wbr>------------------------------<wbr>--------------------<br>
This message and its attachments are strictly confidential. If you are<br>
not the intended recipient of this message, please immediately notify<br>
the sender and delete it. Since its integrity cannot be guaranteed,<br>
its content cannot involve the sender's responsibility. Any misuse,<br>
any disclosure or publication of its content, either whole or partial,<br>
is prohibited, exception made of formally approved use<br>
------------------------------<wbr>------------------------------<wbr>-----------<br>
</blockquote></div><br></div>