c++ - Efficiently compare each element of a numeric vector to the previous element -
I am trying to generate interprinted values - a vector that says, 20, 30, 69, 89, 200, what is the difference between each pair?
There are 25 m elements in the dataset, so I looked at both R and RCPP for the solution - speed is important R implementation:
intertime & lt; - function (x) {output & lt; - x [2: length (x)] - x [1: (length (x) -1) Return (output)} C ++ implementation: < Pre> Numeric Vector Intertium (Numeric Vector Timestamp) {// Identify the size of Input Object Input_Size = Timestamp size (); // Quick output object numeric vector output (input_sas-1); // Loop on data for input (int i = 1; i & lt; input_size; ++ i) {// for each entry, output value [entry] minus [previous entry] output [i-1] = ( Timestamp) [ii] - timestamp [i-1]; } // return refund output; } The C ++ implementation is 1 command of magnitude faster than the implementation. I appreciate that R has been optimized for vectorized operations, so the speed difference is so small that that should not come too much, but: anybody can operate these types of operations Can RCPP / C ++ think in a decent manner that is more efficient?
What do you want to do in standard C ++:
#include & lt; Iostream & gt; # Include & lt; Numerical & gt; int main () {int val [] = {1,2,3,5,9,11,12}; Complete results [7]; Std :: adjacent_difference (Val, Val +7, results); For (auto i: results) std :: cout & lt; & Lt; I & lt; & Lt; ""; Std :: cout & lt; & Lt; Std :: endl; }
Comments
Post a Comment