简体   繁体   中英

optimizing rcpp code

I started trying to use rcpp to improve the speed of a for loop in R where each iteration depends on the previous (ie no easy vectorization). My current code (below) is a bit faster than R but no nearly as fast as I would have thought. Any glaring inefficiencies in the code below that someone can spot? Any general (or specific) advice would be helpful.

UpdateInfections <- cxxfunction(signature(pop ="data.frame",inds="integer",alpha="numeric",t="numeric"), '
DataFrame DF(pop);
IntegerVector xinds(inds);
NumericVector inf_time = DF["inf.time"];
IntegerVector loc = DF["loc"] ;
IntegerVector Rind = DF["R.indiv"] ;
NumericVector infector = DF["infector"] ;
IntegerVector vac = DF["vac"] ;
NumericVector wts(loc.size());
double xt = Rcpp::as<double>(t);
double xalpha = Rcpp::as<double>(alpha);


RNGScope scope;         // Initialize Random number generator
Environment base("package:base");
Function sample = base["sample"];
int n = loc.size();
int i;int j;int k;
int infsize = xinds.size();

for (i=0;i<infsize;i++) {
   int infpoint = xinds[i]-1;
    NumericVector inf_times_prop(Rind[infpoint]);
    NumericVector inf_me(Rind[infpoint]);

for (j=0; j<n;j++){
  if (j == infpoint){
wts[j] = 0.0;
  } else if (loc[j] == loc[infpoint]){
    wts[j] = 1.0;
  } else {
wts[j] = xalpha;
  }
}

inf_me = sample(n,Named("size",Rind[infpoint]),Named("prob",wts));
//Note that these will be shifted by one

for (k=0;k<Rind[infpoint];k++){
  inf_times_prop[k] = floor(::Rf_rlnorm(1.6,.6) + 0.5 + xt);
  if (inf_times_prop[k] < inf_time[inf_me[k]-1] && vac[inf_me[k]-1] == 0){
    inf_time[inf_me[k]-1] = inf_times_prop[k];
    infector[inf_me[k]-1] = inf_me[k];
  }
}
}

// create a new data frame
Rcpp::DataFrame NDF =
Rcpp::DataFrame::create(Rcpp::Named("inf.time")=inf_time,
                        Rcpp::Named("loc")=loc,
                        Rcpp::Named("R.indiv")=Rind,
                        Rcpp::Named("infector")=infector,
                        Rcpp::Named("vac")=vac);
return(NDF);
' , plugin = "Rcpp" )

You are calling back to R. That cannot be as fast a pure C++ solution.

Your example is also long, too long. I recommend profiling and optimizing individual pieces. There is, alas, still no entirely free lunch.

We're actually working on a pure C++ sample function for RcppArmadillo right now. Take a look here http://permalink.gmane.org/gmane.comp.lang.r.rcpp/4179 or here http://permalink.gmane.org/gmane.comp.lang.r.rcpp for updates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM