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();
그럼 I 클리어 미리 크기 벡터에 결과를 저장하는 다양한 타이밍 된 테스트를 실행
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_;
};
,691,363 : 실행 사이에, 예를 들어, 참고로
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
토크 나이split
보다 느린 너무 많이가, 한 - 반복 그림은 심지어 문자열 사본을 포함하지 않습니다.
프로필을 작성하면 Google에 알려줍니다. –
두 번째 것은 메모리 할당을 수행하지 않는 것처럼 보이므로 더 빠를 것이라고 생각합니다. 그래도 한 가지 방법 밖에 없습니다. –
[Boost.Spirit] (http://www.boost.org/libs/spirit/). [Qi] (http://www.boost.org/libs/spirit/doc/html/spirit/qi/tutorials) /quick_start.html)은 두 가지 모두에서 큰 폭의 성능을 보입니다. – ildjarn