मुझे clang++ -O3 -std=c++11 -stdlib=libc++
का उपयोग करके अलग-अलग परिणाम मिलते हैं।
सबसे पहले मैं ~ 470k एक विशाल स्ट्रिंग में कोई नई-पंक्तियों के साथ अल्पविराम के द्वारा अलग शब्दों के साथ एक पाठ फ़ाइल निकाले, तो जैसे:
path const inputPath("input.txt");
filebuf buf;
buf.open(inputPath.string(),ios::in);
if (!buf.is_open())
return cerr << "can't open" << endl, 1;
string str(filesystem::file_size(inputPath),'\0');
buf.sgetn(&str[0], str.size());
buf.close();
तब मैं विभिन्न समय को मंजूरी दे दी एक पूर्व आकार वेक्टर में परिणाम भंडारण परीक्षण भाग गया
struct timed
{
~timed()
{
auto duration = chrono::duration_cast<chrono::duration<double, ratio<1,1000>>>(chrono::high_resolution_clock::now() - start_);
cout << setw(40) << right << name_ << ": " << duration.count() << " ms" << endl;
}
timed(std::string name="") :
name_(name)
{}
chrono::high_resolution_clock::time_point const start_ = chrono::high_resolution_clock::now();
string const name_;
};
: रन के बीच, उदाहरण के लिए,
void vectorStorage(string const& str)
{
static size_t const expectedSize = 471785;
vector<string> contents;
contents.reserve(expectedSize+1);
...
{
timed _("split is_any_of");
split(contents, str, is_any_of(","));
}
if (expectedSize != contents.size()) throw runtime_error("bad size");
contents.clear();
...
}
संदर्भ के लिए, टाइमर सिर्फ इस है 210
मैंने एक एकल पुनरावृत्ति (कोई वेक्टर) भी देखा। यहाँ परिणाम हैं:
{
string word;
word.reserve(128);
timed _("tokenizer");
boost::char_separator<char> sep(",");
boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
for (auto range : tokens)
{}
}
{
string word;
timed _("split iterator");
for (auto it = make_split_iterator(str, token_finder(is_from_range(',', ',')));
it != decltype(it)(); ++it)
{
word = move(copy_range<string>(*it));
}
}
सुस्पष्ट निष्कर्ष: split
का उपयोग
Vector:
hand-coded: 54.8777 ms
split is_any_of: 67.7232 ms
split is_from_range: 49.0215 ms
tokenizer: 119.37 ms
One iteration:
tokenizer: 97.2867 ms
split iterator: 26.5444 ms
split iterator back_inserter: 57.7194 ms
split iterator char copy: 34.8381 ms
tokenizer तो धीमीsplit
की तुलना में ज्यादा है, एक यात्रा आंकड़ा भी स्ट्रिंग प्रतिलिपि शामिल नहीं है।
प्रोफ़ाइल और आप हमें बताएं। –
दूसरा ऐसा लगता है कि यह कोई स्मृति आवंटन नहीं करता है, इसलिए मुझे लगता है कि यह तेज़ होगा। हालांकि, सुनिश्चित करने के लिए केवल एक ही रास्ता है। –
[बूस्ट। पाइटिट] (http://www.boost.org/libs/spirit/)। [क्यूई] (http://www.boost.org/libs/spirit/doc/html/spirit/qi/tutorials /quick_start.html) बड़े मार्जिन दोनों से बेहतर प्रदर्शन करेगा। – ildjarn