I'm looking to speed up an Rcpp function I have written.
The function takes in a string called vcfield, which takes the format od x:y:z. This string has 3 fields if you separate it by the :. It takes an int DSfield which tells us the index (0 based) field from vcfield which is the dosage. Dosage is always one double value.
It also takes in 2 ints called aa and bb
The program then returns the value of abs[(aa + bb) - dosage].
So calling ReturnUncertainty("1/1:1.88:0,0.12,0.88", 1, 1, 1) should return 0.12, as abs[(1+1) - 1.88] = 0.12
We should presume that the number of characters in the different fields of vcfield might vary. So it should be also be capable of taking a value like "1/1/1/1:1.88999:0,0.12,0.88".
#include <Rcpp.h>
#include <bits/stdc++.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
double ReturnUncertainty(String vcfield, int DSfield, int aa, int bb) {
string stdfield = vcfield;
vector <string> tokensMain;
stringstream checkMain(stdfield);
string intermediate;
while (getline(checkMain, intermediate, ':')) {
tokensMain.push_back(intermediate);
}
std::string DS = tokensMain[DSfield];
double DSf;
std::istringstream(DS) >> DSf;
double aaD = static_cast<double>(aa);
double bbD = static_cast<double>(bb);
double genoSum = aaD + bbD;
return abs(genoSum - DSf);
}
QUESTION: As a total newbie to C++, I was wondering whether or not I am doing anything that is very inefficient / unecessary which might be slowing my code down?