37% is a noticeable difference. I wonder what sort of optimization flags you played with in your code.
Printable View
37% is a noticeable difference. I wonder what sort of optimization flags you played with in your code.
Yes, but if you read some of the other posts, I have a pretty old compiler, and the newer compilers are actually much closer (and vector is even faster in some of the examples).
Also, my code is the WORST possible scenario for vector, as it does so very little with the value it fetches from the vector.
--
Mats
I don't remember the entire thread and didn't read through it, but wasn't that a comparison between vector and a statically sized array?
We're discussing vector versus a dynamic array with new[]/delete[] here.
Doesn't stack memory work better with your CPU cache, and that's why arrays are faster that dynamic arrays? or is it just the calls to new/delete that slow down dynamic arrays?
In either case, calling vector::reserve() before you start inserting elements can reduce your new/delete calls to just 1.
In the case of replacing new[]/delete[] with vector, you should be able to set an initial size in your vector constructor (just like you do when calling new[]). So there would be no need to call reserve or do any other tricks to reduce the memory allocations.
Whether it slows anything down depends on what the array is holding. I guess I'm just talking about a simple replacement that involves using vector and getting rid of the need for delete. I'd consider reserve instead of the constructor to be a premature optimization in this thread.