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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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…