I'm working with long std::string(std::wstring) processing
The length of original string can be 10 to 1 million, and I got a number of substring offsets. what I need is to concatenate of multiple substrings of original string and with some new strings.
Except using using string append
auto str = new std::string();
str.append(original.substr(a,b)).append(newstr1).append(original.substr(c,d)).append.....
Is there any more efficient way like handling pointer or string iterators?
Thanks.
UPDATE:
I got several feedbacks now, except for rope I can test all the other methods The result is following:
#include <string>
#include <iostream>
#include <chrono>
#include <ctime>
std::string GetSystemTimeEpoch(){
using namespace std::chrono;
auto now = system_clock::now();
time_point<system_clock> epoch;
microseconds ms = duration_cast<milliseconds>(now - epoch);
double epoch_time = (unsigned long long)ms.count() / 1000000.0;
unsigned long long postfix = (unsigned long long)ms.count() % 1000000;
std::time_t time = static_cast<time_t>(epoch_time);
std::tm tm = *std::localtime(&time);
char Buf[80];
std::strftime(Buf, sizeof(Buf), "%Y-%m-%dT%H:%M:%S", &tm);
std::string finaltime(Buf);
return finaltime.append(".").append(std::to_string(postfix));
}
#define TESTLENGTH1 1000000000
#define TESTLENGTH2 300000000
int main(){
std::string Str(TESTLENGTH2, 'c');
std::cout << GetSystemTimeEpoch() << " Begin of Method 1(replace)"<< std::endl;
for (size_t i = 0; i < Str.length(); i++){
Str.replace(i, 1, "d");
}
std::cout << GetSystemTimeEpoch() << " Begin of Method 2(append)" << std::endl;
std::string NewStr1;
for (size_t i = 0; i < Str.length(); i++){
NewStr1.append(Str.substr(i, 1));
}
std::cout << GetSystemTimeEpoch() << " Begin of Method 3(+=)" << std::endl;
std::string NewStr2;
for (size_t i = 0; i < Str.length(); i++){
NewStr2 += Str.substr(i, 1);
}
std::cout << GetSystemTimeEpoch() << " Begin of Method 4(reserve)" << std::endl;
std::string NewStr3;
NewStr3.reserve(TESTLENGTH2);
for (size_t i = 0; i < Str.length(); i++){
NewStr3 += Str.substr(i, 1);
}
std::cout << GetSystemTimeEpoch() << " End" << std::endl;
return 0;
}
===
2016-05-21T22:38:51.471000 Begin of Method 1(replace)
2016-05-21T22:38:58.972000 Begin of Method 2(append)
2016-05-21T22:39:14.429000 Begin of Method 3(+=)
2016-05-21T22:39:29.944000 Begin of Method 4(reserve)
2016-05-21T22:39:44.892000 End
Press any key to continue . . .
It seems the quickest way is not doing concatenation but do replace instead for the concatenation.(method1)
Concatenation methods(2,3,4) there seems no difference.
I have not tested sgi ROPE class since I could not find a beginners document to start with :). If someone know about it, please leave a sketch or complete this testcase.
PS. TESTLENGTH1 crashed for method 2 and 3 and 4
PS2. Testing environment, Win7x64;VC++2013;Target Win32,Release. i5 2GHz,8GB RAM