How to benchmark in-place functions?

I suspect you’re seeing an artifact of the amortized constant time of push! (see algorithms - Why is push_back in C++ vectors constant amortized? - Computer Science Stack Exchange for some discussion). The longer the vector gets, the less frequently its capacity is changed, so the more likely it is that your benchmark loop will happen to catch a set of pushes that do not contain a capacity change (and thus don’t contain any memory allocation).

In other words: don’t worry about those few extra nanoseconds and the one allocation here :slight_smile:

1 Like