Possible Duplicate:
std::vector is so much slower than plain arrays?
Looks like vector is allocated on heap instead of stack.
So should I consider using array to replace vector (if possible) when performance becomes a serious issue?
No. (to satisfy the comment pedants, no, you should not "prefer" arrays over vectors for performance, but sure, you should "consider" using arrays to replace vectors, for the specific cases outlined below)
When performance becomes a serious issue, you should base your optimizations on actual data, not second-hand stories and hearsay and superstition.
If replacing your vector with an array gives you a measurable (and necessary) speedup, then you should do it.
But note that you can only use a stack-allocated array if:
In most cases, these conditions won't be true, and then the array would have to be heap-allocated anyway, and then you just lost the one advantage arrays had.
But if all those conditions are true and you can see that this heap allocation is actually hurting your performance measurably, then yes, switching to an array (or a std::array
) makes sense.
Otherwise? No...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With