Jump to content

  • Log In with Google      Sign In   
  • Create Account

Using old array or stl


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
10 replies to this topic

#1   Members   -  Reputation: 148

Like
0Likes
Like

Posted 19 August 2014 - 01:26 PM

Hi all what is better(Talking about memory and performance spped ) for store the  float vertex   using std::vector  vrtx<float> or use a raw array / like  float vrtx[]

 

thanks in advance 



#2   Moderators   -  Reputation: 9448

Like
1Likes
Like

Posted 19 August 2014 - 01:31 PM

Yes

Edited by fastcall22, 19 August 2014 - 02:17 PM.

gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=

#3   Members   -  Reputation: 849

Like
0Likes
Like

Posted 19 August 2014 - 01:52 PM

Yes, I agree with fastcall22.



#4   GDNet+   -  Reputation: 13872

Like
0Likes
Like

Posted 19 August 2014 - 04:46 PM

std::vector, under the covers, is just a managed array -- it has precisely the same layout in memory as a native array, or a contiguous block of dynamically-allocated memory, with the benefit that you neither have to allocate it statically (native array) or remember to delete it (dynamically-allocated memory). In C++ 11, std::vector (and other containers) also support move semantics which, properly afforded and called upon, eliminates redundant linear copy operations which a raw array cannot avoid AFAIK (perhaps as a particularly aggressive compiler optimization, given particularly formatted source code) -- A real-world example of this would be a function that returns the contents of a locally-declared array (which will need to be copied by the calling code), vs the same function which returns a locally-declared std::vector, which will simply move the std::vector internals between the locally-defined vector and the vector in the calling code, provided that either the return type is an rvalue reference (&&) itself, or std::move is invoked at the call-site.

 

In general, always prefer the *correct* standard container over native arrays and dynamic memory allocation -- there is some skill in choosing the right container based on your usage, and in configuring your build to not leave performance on the table (e.g. disable checked iterators) but the standard (or some other well-worn library) containers are always the right default choice -- they are faster, more robust, and contain far fewer bugs than anything you could hope to write yourself. There are times that they are not appropriate, but by the time you are ready to forego them in favor of some other solution, you will be experienced enough to not need to seek advice on these forums smile.png


Edited by Ravyne, 19 August 2014 - 04:50 PM.

throw table_exception("(ノ ゜Д゜)ノ ︵ ┻━┻");


#5   Crossbones+   -  Reputation: 16201

Like
0Likes
Like

Posted 19 August 2014 - 05:43 PM

If the array is of a fixed size, prefer std::array over std::vector, but otherwise yes always use an STL container rather than a C array unless you have very strong reason not to.

You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access. I don't recall if std::array throws on out of bounds access, but I'd imagine it does. Debug builds will also have a higher cost due to the lack of inlining of the various accessor functions the STL containers use to emulate the arrays.

These perf differences are purely academic at the scale of game/app you're likely working on, though. Big AAA titles need to squeeze out the difference but then they still use STL-like containers; they just replace them with their own performance-conscious versions (which you shouldn't attempt unless you're ready to waste months of time in learning, development, test writing, etc.).

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!


#6   GDNet+   -  Reputation: 13872

Like
0Likes
Like

Posted 19 August 2014 - 06:26 PM


You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access.

 

Only for random access (e.g. [] brackets) though, and only if exceptions are enabled, right? I would assume, perhaps incorrectly, that linear access through iterators at least amortizes the cost of two checks, if not avoiding it entirely -- perhaps modulo any funny-business one might be doing with the array size whilst walking it.


throw table_exception("(ノ ゜Д゜)ノ ︵ ┻━┻");


#7   Crossbones+   -  Reputation: 12115

Like
0Likes
Like

Posted 19 August 2014 - 06:56 PM

For storing float vertices the answer is neither.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#8   Crossbones+   -  Reputation: 16201

Like
0Likes
Like

Posted 19 August 2014 - 10:39 PM

Only for random access (e.g. [] brackets) though, and only if exceptions are enabled, right? I would assume, perhaps incorrectly, that linear access through iterators at least amortizes the cost of two checks, if not avoiding it entirely -- perhaps modulo any funny-business one might be doing with the array size whilst walking it.


Iterators are typically a bit worse in many respects - at least on non-optimized builds - as most STL implementations ship with complex "checked iterators" enabled by default. Different STLs have different ways of turning these off. One of the better reasons I have for writing a custom vector is just to ensure the iterators are plain ol' pointers, which can for some architectures turn your debug game from a slideshow into something almost playable. smile.png

I am so looking forward to getting to use VS2013's new /Zo option at work and seeing how it holds up. I'd really like something like GCC's -Og, but I guess I'll have to keep on waitin'.

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!


#9   Moderators   -  Reputation: 10607

Like
1Likes
Like

Posted 20 August 2014 - 01:38 AM


You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access. I don't recall if std::array throws on out of bounds access, but I'd imagine it does.

std::vector::at throws an exception, but std::vector::operator[] does not. Some light Googling indicates that std::array is similar.

 

Most implementations of the C++SL will do range checking, e.g. as an assertion, in debug builds, I believe at least some versions of VC++ went for range checking in all builds, but with a separate flag to disable them.



#10   Crossbones+   -  Reputation: 8054

Like
0Likes
Like

Posted 20 August 2014 - 05:47 AM


std::vector::at throws an exception, but std::vector::operator[] does not. Some light Googling indicates that std::array is similar.

Yes, an implementation that does range checking and throws an exception for operator[] of the standard containers is non-conforming.  Most implementations are conforming in this respect, with an optional "debug" mode that needs to be explicitly enabled but is explicitly non-conforming.  At least one well-known vendor is non-conforming by default and the developer needs to go out of her way to make the implementation conform to the standard by flipping switches and pre-defining constants, which leads to lots of questions about poor performance in forums like these.


Stephen M. Webb
Professional Free Software Developer

#11   Members   -  Reputation: 148

Like
0Likes
Like

Posted 20 August 2014 - 06:42 AM

thanks to all 4 the quick response :)






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS