Reputation: 1012
I am interested to get a substring and then convert that into a long int
for further processing. I need to do this for a large number of strings. Currently what I do is using .substr()
, as shown in the following example Test.
// Example program
#include <iostream>
#include <string>
int main()
{
std::string content = "123421341234432231112343212343";
unsigned long int sub = atol(content.substr(0,18).c_str());
std::cout << "sub: " << sub << '\n';
return 0;
}
I want to know the fastest way to do this. It's not always .substr(0,18)
, it can be anything of length 18 (remaining length if not 18) .substr(i,18)
.
Edit:
About the number of strings, roughly 30 million, about fast ( i think to get a substring copy and then converting to long int is a slow process. I want it to be faster than .substr()
method). To be honest, I want it to be as fast as it can be.
Actually, the strings are in a fasta
file which I read each at a time and remove the unwanted content by boost::split() and store the wanted content. Then I need to do different passes of getting different substrings of the string for further processing.
Upvotes: 0
Views: 2257
Reputation: 67743
Get substring and convert to long integer in a fastest way
... is almost certainly the wrong question.
With the caveats that you should always measure first, and should know what performance you actually need, and that you haven't really given us enough information to help with those:
creating strings and substrings in your current form is likely to be much more expensive than the integer conversion, so you're worrying about the wrong thing. Profiling first would have shown this.
So (after profiling and assuming I guessed correctly), start by eliminating the copying and dynamic allocation: stop using std::string
and substr
entirely. Work directly on the raw buffer.
Upvotes: 2