r/cpp • u/Little-Reflection986 • Feb 16 '26
Favorite optimizations ??
I'd love to hear stories about people's best feats of optimization, or something small you are able to use often!
135
Upvotes
View all comments
Show parent comment
1
u/Both_Helicopter_1834 Feb 16 '26
Taco kid sez, why not both:
void fm(vector<T> &v);
inline vector<T> f(vector<T> const &v) { vector<T> v_{v}; fm(v_); return v_; }
Or, if the function is doing something like a merge sort:.
// out2 may be orig. If out1 is empty, result is in out2.
void f_help(vector<T> const &orig, vector<T> &out1, vector<T> &out2);
inline void fm(vector<T> &v) { vector<T> tmp; f_help(v, tmp, v); if (v.empty()) v = move(tmp); }
inline vector<T> f(vector<T> const &v) { vector<T> tmp[2]; f_help(v, tmp[0], tmp[1]); if (tmp[0].empty()) return tmp[1]; return tmp[0]; }