Oliver Keyes
Oliver Keyes

Reputation: 3294

Efficiently compare each element of a numeric vector to the previous element

I'm trying to generate intertime values - given a vector of say, 20, 30, 69, 89, 200, what's the difference between each pair?

The dataset has 25m elements, so I looked at both R and RCpp for a solution - speed is important. The R implementation was:

intertime <- function(x){

    output <- x[2:length(x)] - x[1:(length(x)-1)]
    return(output)
}

The C++ implementation:

NumericVector intertime(NumericVector timestamps) {

  //Identify size of input object
  int input_size = timestamps.size();

  //Instantiate output object
  NumericVector output(input_size-1);

  //Loop over the data
  for(int i = 1; i < input_size;++i){

    //For each entry, the output value is [entry] minus [previous entry]
    output[i-1] = (timestamps[i] - timestamps[i-1]);

  }

  //Return
  return output;
}

The C++ implementation is ~1 order of magnitude faster than the R implementation. I appreciate that R is kind of optimised for vectorised operations, so the speed difference being so small shouldn't shock me that much, but: can anyone think of a decent way of performing these kind of operations within Rcpp/C++ that's more efficient?

Upvotes: 4

Views: 829

Answers (1)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42909

In standard C++ there's std::adjacent_difference for doing what you want:

#include <iostream>
#include <numeric>

int main () {
  int val[] = {1,2,3,5,9,11,12};
  int result[7];

  std::adjacent_difference (val, val+7, result);
  for(auto i : result) std::cout << i << " ";
  std::cout << std::endl;
}

LIVE DEMO

Upvotes: 3

Related Questions