title: RcppParallel code runs slower than pure Rcpp code
body: <p>I want to compute the distance of a specific origin point to a set of other points. I tried parallelizing my code using <code>RcppParallel</code>, but the parallelized code seems to run much slower than the code written in pure <code>Rcpp</code>.</p>
  
  <p>This is the code that can be compiled easily in Rstudio:</p>
  
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;cmath&gt;
  #include &lt;RcppArmadillo.h&gt;
  #include &lt;RcppParallel.h&gt;
  #include &lt;functional&gt;
  using namespace RcppParallel;
  
  // Define worker to be used with RcppParallel
  struct ComputeDistances : public Worker{
  
    // Training data
    const RMatrix&lt;double&gt; trainXs;
  
    // Point to compute distances from
    const RVector&lt;double&gt; point;
  
    // Destination vector
    RVector&lt;double&gt; distances;
  
    // initialize with source and destination
    ComputeDistances(const Rcpp::NumericMatrix trainXs, const Rcpp::NumericVector point, Rcpp::NumericVector distances)
      : trainXs(trainXs), point(point), distances(distances) {}
  
    void operator()(std::size_t begin, std::size_t end) {
  
      for (std::size_t i = begin; i &lt; end; i++){
  
  
        RMatrix&lt;double&gt;::Row some_point = trainXs.row(i);
        std::vector&lt;double&gt; temp(some_point.length());
  
        // Compute Euclidean Distance
        std::transform(point.begin(), point.end(), some_point.begin(), temp.begin(), std::minus&lt;int&gt;());
        std::transform(temp.begin(), temp.end(), temp.begin(), temp.begin(), std::multiplies&lt;int&gt;());
        int dist = sqrt(std::accumulate(temp.begin(), temp.end(), 0));
        distances[i] = dist;
  
      }
  
    }
  
  };
  
  // Computing the distances in pure Rcpp
  // [[Rcpp::export]]
  arma::vec compdist(arma::mat data, arma::rowvec point){
  
    arma::vec distances = arma::zeros(data.n_rows);
  
    for (unsigned int i = 0; i &lt; data.n_rows; i++){
      double dist = sqrt(arma::sum(arma::pow((data.row(i) - point),2)));
      distances[i] = dist;
    }
  
    return distances;
  
  }
  
  // Compute the distances using the parallelized code
  // [[Rcpp::export]]
  arma::vec compdistParallel(arma::mat data, arma::rowvec point){
  
      Rcpp::NumericMatrix DATA = Rcpp::as&lt;Rcpp::NumericMatrix&gt;(Rcpp::wrap(data));
      Rcpp::NumericVector POINT = Rcpp::as&lt;Rcpp::NumericVector&gt;(Rcpp::wrap(point));
      Rcpp::NumericVector DISTANCES(data.n_rows);
      ComputeDistances computedistances(DATA, POINT, DISTANCES);
      parallelFor(0, DATA.nrow(), computedistances);
  
      arma::vec distances = Rcpp::as&lt;arma::vec&gt;(Rcpp::wrap(DISTANCES));
  
      return distances;
  }
  </code></pre>
  
  <p>These are the R benchmarks:</p>
  
  <pre><code>library(rbenchmark)
  data = matrix(rnorm(10000000), nrow = 100000, ncol = 100)
  point  = matrix(rnorm(100), nrow = 1, ncol = 100)
  res = benchmark(compdist(data, point),
                  compdistParallel(data, point), replications = 3)
  </code></pre>
  
  <p>with the given results:</p>
  
  <pre><code>&gt; res
                             test replications elapsed relative user.self sys.self user.child sys.child
  1         compdist(data, point)            3   0.164    1.000     0.097    0.068          0         0
  2 compdistParallel(data, point)            3   0.316    1.927     0.503    0.145          0         0
  </code></pre>
  
  <p>So I am wondering why is my code slower when parallelized? Is there something wrong with my implementation? </p>