C++ vector performance: iterator access versus brackets operator access -
this question has answer here:
i'm wondering whether iterator outperform brackets operator in sequential access setting. consider following code snippets:
// v1.1 (std::vector<int>::const_iterator = v.begin(); != v.end(); ++it) { do_something(*it); } // v1.2 (size_t = 0; != v.size(); ++i) { do_something(v[i]); } // v1.3 std::vector<int>::const_iterator endit = v.end(); (std::vector<int>::const_iterator = v.begin(); != endit; ++it) do_something(*it); } // v1.4 size_t size = v.size(); (size_t = 0; != size; ++i) { do_something(v[i]); } // v2.1 (std::vector<int>::iterator = v.begin(); != v.end(); ++it) { *it = - v.begin(); // iterator arithmetic; expensive or not? } // v2.2 (size_t = 0; != v.size(); ++i) { v[i] = i; }
has ever done benchmark these? (well, depends on compiler; let's take g++ 4.7 or 4.8 instance.) thanks.
Comments
Post a Comment