Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:03:24+00:00 2026-06-14T09:03:24+00:00

I started trying to use rcpp to improve the speed of a for loop

  • 0

I started trying to use rcpp to improve the speed of a for loop in R where each iteration depends on the previous (i.e. 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" )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:03:25+00:00Added an answer on June 14, 2026 at 9:03 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the following code to write out all processes started
I've just started programming, therefore I'm kinda noob. I'm trying to use python to
I have recently started to use Twitter Bootstrap and trying to understand how it
I started by trying to add a .toggle() to this code so that the
I started trying to use sqlite to host small websites and on my own
I have started trying to use SDL in Xcode 4.3.2 so I started up
So, I recently started trying to use an XNA program to start another XNA
Trying to use Database Designer in VS2010 for the first time. I started adding
I've just started trying to use Solr, and already I think that I'm attempting
Just started writing java / android and I am trying to use android.widget.VideoView. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.