Reputation: 22500
Is there a performance penalty for working with a vector from the standard library in C++ instead of arrays in C?
Upvotes: 2
Views: 1251
Reputation: 641
The only real difference is that accesses with std::vector go through trivial functions. So long as you're using an appropriate optimization level such that those function calls get inlined, they'll be the same.
Upvotes: 1
Reputation: 9301
No, there's not (provided you compile with optimization so inlining can happen), provided you mean dynamically sized C "arrays" obtained with malloc.
Fixed-sized arrays in C will have the slight advantage that their address is fixed after linking (if global), or that they live directly on the stack rather than indirectly through a pointer to somewhere on the heap. I do believe there is still no performance difference; constant base addresses aren't faster than variable ones; both get loaded into a CPU register.
Upvotes: 7