473,748 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preserving capacity of a vector

For my renderer at each frame I'm filling a vector with CVisual* elements,
sorting it, rendering the objects and then clearing the vector again.

I suspect there will be an overhead if the vector is resizing it's capacity
all the time... is there a way to prevent this?

--
Lasse
Jul 19 '05 #1
9 2960
"Lasse Skyum" <lskyum(AT)mail .dk> wrote in message
news:3f******** *************** @dread12.news.t ele.dk...
| For my renderer at each frame I'm filling a vector with CVisual* elements,
| sorting it, rendering the objects and then clearing the vector again.
|
| I suspect there will be an overhead if the vector is resizing it's
capacity
| all the time... is there a way to prevent this?

An std::vector instance will NOT release the memory it has allocated
when its size is reduced, or when it is emptied ( by calling its
clear() member ). So resizing a vector of pointers can only trigger
a memory reallocation if its new size is larger than what it has
ever been - the capacity is preserved by default.

For the elements that are added/removed by calling resize(), there
can still be, however, a small overhead for initializing/destroying
elements (but for pointers, the destructions will be optimized out
on a good library implementation) .

Regards,
Ivan
--
http://ivan.vecerina.com
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #2
> For my renderer at each frame I'm filling a vector with CVisual*
elements,
sorting it, rendering the objects and then clearing the vector again.

I suspect there will be an overhead if the vector is resizing it's capacity all the time... is there a way to prevent this?


Suspecting something is a poor foundation for optimization efforts. Make
sure your code is correct first, optimize (using profiling tools to
identify the bottlenecks) later. That being said, resizing or clearing
typically does not affect the vectors capacity. So there is no need for
you to prevent this as it does not happen. Also if you know how big the
vector is going to be in advance, it is a good idea to call
std::vector::re serve() with the anticipated size. Note that
std::vector::re serve() only increases the capacity of the vector, the
capacity of a vector is never decreased by a std::vector::re serve()
regardless of the number passed to this function.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl

Jul 19 '05 #3
On Mon, 27 Oct 2003 14:01:24 +0100, "Ivan Vecerina"
<NONE_use_form@ website_to_cont act_me> wrote:
"Lasse Skyum" <lskyum(AT)mail .dk> wrote in message
news:3f******* *************** *@dread12.news. tele.dk...
| For my renderer at each frame I'm filling a vector with CVisual* elements,
| sorting it, rendering the objects and then clearing the vector again.
|
| I suspect there will be an overhead if the vector is resizing it's
capacity
| all the time... is there a way to prevent this?

An std::vector instance will NOT release the memory it has allocated
when its size is reduced, or when it is emptied ( by calling its
clear() member ).


.... unless you are using MSVC 7.1. Dinkumware made clear() release all
storage in response to customer demand. Apparently they're going to
put it back the way the standard seems to imply (sticky capacity) in
the next version.

Tom
Jul 19 '05 #4
... unless you are using MSVC 7.1. Dinkumware made clear() release all
storage in response to customer demand. Apparently they're going to
put it back the way the standard seems to imply (sticky capacity) in
the next version.


Oki, sticky capacity seems to be what I need... but this leads me to another
question: Can't I reset the capacity somehow if I want to?

I use std::vector for object-buckets on a map (for a game) and it seems
silly to hold a huge capacity somewhere just because there once was a lot of
objects there.

--
Lasse
Jul 19 '05 #5
... unless you are using MSVC 7.1. Dinkumware made clear() release all storage in response to customer demand. Apparently they're going to
put it back the way the standard seems to imply (sticky capacity) in
the next version.
Oki, sticky capacity seems to be what I need... but this leads me to

another question: Can't I reset the capacity somehow if I want to?

I use std::vector for object-buckets on a map (for a game) and it seems silly to hold a huge capacity somewhere just because there once was a lot of objects there.


IIRC the book "Effective STL" from Scott Meyers recommends swapping
vectors to reduce capacity.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 19 '05 #6
(...) optimize (using profiling tools to
identify the bottlenecks) later.


Can you or anybody else recommend some tools to do such optimisation? Are
there any free/open source applications to find bottlenecks in a program you
are aware of?

Thanks Phil
Jul 19 '05 #7
IIRC the book "Effective STL" from Scott Meyers recommends swapping
vectors to reduce capacity.


Okay, seems a little "hack" style... is it guarenteed to work by the
standard? And does it work in practice?

--
Lasse

Jul 19 '05 #8
> > (...) optimize (using profiling tools to
identify the bottlenecks) later.
Can you or anybody else recommend some tools to do such optimisation?


The tools don't do the optimization, they only tell you where in the
code is most time spend. Using this information you can determine which
for part of the code optimization will make a difference.
Are there any free/open source applications to find bottlenecks in a program you are aware of?


It depends on your development environment which profilers you can use.
G++ comes with GProf, MSVC comes which its own profiler (though I
believe only in the Enterprise edition). AQTime can handle quite a few
development environments on the PC platform
http://www.automatedqa.com/products/aqtime.asp but is not free (but
quite affordable for professional use). If you are running on a Intel
platform and willing to spend some dollars V-Tune
(http://www.intel.com/software/products/vtune/) might be an
consideration

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl

Jul 19 '05 #9
"Lasse Skyum" <lskyum(AT)mail .dk> wrote in message
news:3f******** *************** @dread12.news.t ele.dk...
| > IIRC the book "Effective STL" from Scott Meyers recommends swapping
| > vectors to reduce capacity.

The specific idiom is:
vector<ItemType >().swap( vectorToBeClear ed );

| Okay, seems a little "hack" style... is it guarenteed to work by the
| standard?
No formal/absolute guarantee.

| And does it work in practice?
Yes. Implementations where this wouldn't work are hard to imagine
given some constraints imposed in the standard (i.e. iterators are
not to be invalidated by a swap operation, IIRC).
Regards,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2404
by: Lasse Skyum | last post by:
We had a thread earlies about how to clear the capacity af a std::vector... how about this: std::vector<int> v; v.~vector<int>(); // destruct it (don't know if I need the <int> here?) new (&v)vector<int>(); // construct it again --
4
2630
by: Noo | last post by:
Hi. I've got some code that uses vectors fairly extensively and it needs to be efficient. Therefore I'm using reserve() quite a bit. However what other functions (are supposed to) change the capacity? I know that adding extra elements (e.g. by using push_back()) may do this, if extra capacity is needed. On the C++ implementation I'm using: clear() seems to reset the capacity to zero. resize() leaves the capacity as is.
8
2902
by: Joseph Turian | last post by:
Fellow hackers, Is it possible to shrink the capacity of a vector? i.e. Without new'ing and delete'ing a vector, can one return its memory to the heap? Here's what I get under the g++ implementation: vector<double> v = vector<double>(1024*1024);
2
2313
by: Fred Zwarts | last post by:
There are a few things I do not understand about the vector class in de standard C++ library. The description of the resize member function says: void resize(size_type sz);Alters the size of self. If the new size (sz) is greater than the current size, then sz-size() instances of the default value of type T are inserted at the end of the vector. If the new size is smaller than the current capacity, then the vector is truncated by erasing...
16
5137
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
3
2472
by: leeps_my | last post by:
I'm thinking of using "resize-to-size" to do the trimming: aVector.resize( aVector.size( ) ); I'm wondering why Scott Meyers was recommending "swap trick" instead, since the trick involves construction and destruction of a temporary, and might still leave some excess capacity in the vector.
5
9526
by: Naveen | last post by:
I am trying to write a conatiner which has std::map like methods but preserves the order of insertion. TO achieve this I thought of providing my own function for comparing the keys of the map. Following is the sample code for this: template <class T> struct my_compare { bool operator () (const T&a, const T&b) {
3
3298
by: sophia.agnes | last post by:
Dear all, I was going through the book "C a software engineering approach by darnell & Margolis" there was a section named sign preserving vs value preserving it is as follows sign preserving rule:- when signed and un signed objects meet in an expression, the result is
5
4395
by: puzzlecracker | last post by:
Say I have a vector<intintVec, and I reserve some sapce intVec.reserve(100); Is it fair to assume the vector contains enough space for 100 elements. In which case, the capacity -- ability to store 100 elements without a relocation -- is 100. Then what is the difference for intVec.resize(100)?
0
9552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8245
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.