std::vector::reserve performance penalty

后端 未结 6 745
感动是毒
感动是毒 2021-01-05 02:19
inline void add(const DataStruct& rhs) {
   using namespace boost::assign;
   vec.reserve(vec.size() + 3);
   vec += rhs.a, rhs.b, rhs.c;
}

The

6条回答
  •  爱一瞬间的悲伤
    2021-01-05 02:46

    If you profiled the code I bet you would see that the += IS very fast, the problem is the reserve is killing you. You should really only use reserve when you have some knowledge of how big the vector will grow to. If you can guess ahead of time then do ONE reserve, otherwise just go with the default push_back.

提交回复
热议问题