Array vs Vector Discussion :)

Started by
24 comments, last by Decrius 13 years, 6 months ago
Quote:
Picking an array based on the idea that "Its faster in debug mode!"


That is a valid point but I run in debug a lot. But regardless, it still has run faster in release for me every single time as well.

Quote:
Had you presented your original post as a question rather than as fact, we would have gone easier on you.


Fair enough. I added 'tip' in the edit after I had run it. You must have something check marked to get it to optimize vectors better than mine cuz I'm always about 3 times slower.

Quote:
First off, because no-one but the developer is going to see the debug mode. Secondly, don't optimize till it becomes a problem.


Well 26 seconds vs 1/2 second is a problem. So in a respectful way, I as the developer have a problem (26 seconds), that I as the developer see because I see debug mode :(

And I had seen some code with a 3D vector which now I know why it is very slow because it stores like 1 million things. Also the code never called the reserve function :( So, yes it does happen that people still don't reserve vectors.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Advertisement
Quote:Original post by dpadam450
Well 26 seconds vs 1/2 second is a problem. So in a respectful way, I as the developer have a problem (26 seconds), that I as the developer see because I see debug mode :(


If its a game, you have a problem whether its 26 seconds or 1/2 second. That would imply a very bad algorithm that should be the focus of your efforts rather than swapping out std::vector for an array, or possibly using std::vector in a sane manner, since your benchmark doesn't do so.

Again, you're 1) optimizing for a special case (debug mode) not the general case (release mode), 2) focusing on the wrong thing (trying to out-think the compiler rather than using data structures intelligently), 3) using an artificial benchmark to prematurely make performance decisions. If your game is spending 1/2 of a second in vector/array code, then there are larger problems afoot.
The single thing you can rightfully blame std::vector of is the fact that every vector must do at least one heap allocation and if you have more than a dozen or so elements, it must at least do two heap allocations (and will do log(N) allocations if you keep pushing_back without reserving).
On the other hand, declaring int array[10000000]; like in your example takes about 40 MB of stack space, which honestly is a number that very well justifies a heap allocation. In fact, I'm surprised your program doesn't crash, because with default options, you only have a 1 MB stack size, if I remember correctly.

Other than for the allocations, it isn't worth to worry about overhead. While I can't say much about MSVC (since I'm not using it), chances are that it is not optimizing much worse than gcc (this can be safely assumed).
And gcc on the other hand, optimizes most of STL and TR1 into the exact same (and sometimes better) code as carefully hand-tuned specialized code. It is a total waste of time to try and do any better.
Moving to General Programming so we can stop distracting beginners with potentially dangerous and/or wrong ideas, and take off the kid gloves and tell people when they're wrong.
Quote:Original post by dpadam450
That is a valid point but I run in debug a lot. But regardless, it still has run faster in release for me every single time as well.

I'm using Visual Studio 2010 Express Edition. I've used a reasonably vanilla Release mode build with the exception of the following preprocessor definitions at project scope:

  • _HAS_ITERATOR_DEBUGGING=0

  • _SECURE_SCL=0


Some non-performance settings are changed (e.g. generating assembly output), as I use this project for all GD.net related code.
It's funny, C++ is probably the last language that still has arrays.

All others have either collections or dictionaries.

&#106avascript doesn't even have ints.<br><br>And somehow, the world hasn't come to standstill.
Quote:Original post by Antheus
It's funny, C++ is probably the last language that still has arrays.

All others have either collections or dictionaries.

&#106avascript doesn't even have ints.

And somehow, the world hasn't come to standstill.


Not so fast...D still has arrays as well ;). Although they are accessed through two memsize types (not one like C/C++). One for a pointer to the beginning element, and the second to the last or the size of the array. Much like std::vector but then embedded into the syntax. Really neat and convenient I find.

It is also through D I became aware that function parameter sets are actually tuples. And so are structs...just a bundle of different types.

For anything except system-programming I must admit arrays are of less use. I don't see myself surviving without integers yet though...
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

This topic is closed to new replies.

Advertisement