473,471 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

efficiency of std::vector for small arrays.

I am currently writing some code where I have a list of objects with a
maximum occupancy of 4. I have been employing std::vector for this but
I was wondering how much overhead is involved. Is it better to use
some other data structure if speed is critical i.e. maybe a struct
like this-

template <typename T>
struct max_four
{
int element_count;
T elements[4];
};

Or are std::vectors/lists reasonably competitive in both space/time?

Thanks in advance,

Carter.
Jun 27 '08 #1
3 2095
Carter wrote:
I am currently writing some code where I have a list of objects with a
maximum occupancy of 4. I have been employing std::vector for this but
I was wondering how much overhead is involved. Is it better to use
some other data structure if speed is critical i.e. maybe a struct
like this-

template <typename T>
struct max_four
{
int element_count;
T elements[4];
};

Or are std::vectors/lists reasonably competitive in both space/time?
The answer basically depends on how good your dynamic memory mananger
is. I would venture a guess, however, that no dynamic allocation is
better than any dynamic memory allocation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On May 12, 6:58*pm, Carter <carterch...@gmail.comwrote:
I am currently writing some code where I have a list of objects with a
maximum occupancy of 4. I have been employing std::vector for this but
I was wondering how much overhead is involved. Is it better to use
some other data structure if speed is critical i.e. maybe a struct
like this-

template <typename T>
struct max_four
{
* * * int element_count;
* * * T elements[4];

};

Or are std::vectors/lists reasonably competitive in both space/time?
A fixed-size container class such as std::tr1::array<(or
boost::array<>) would no doubt be more efficient than using a
dynamically-sized container such as a std::vector or a std::list. I
doubt however that the overhead saved would be meaningful; you would
have to profile your program's performance to find out for certain.

Greg

Jun 27 '08 #3
Thanks I might give the boost::array<a try.
Jun 27 '08 #4

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

Similar topics

11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
3
by: kayjean | last post by:
Hi. I have a small test.cc >> g++ -o test test.cc (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) >> phase 1 (VSZ RSS 50844 47748 ) >> phase 2 (VSZ RSS 50844 47748 ) >> phase 3 (VSZ RSS...
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
2
by: Priya Mishra | last post by:
Hi All It was very nice to intract with this group, While in my previous post, I was suggested to reffer the link, in order to learn C++, Well I was going thoruigh the link in which i had some...
6
by: slyi | last post by:
Is it ok to assume that the following assertion is valid for all implementations of std::vector?: std::vector<T> v(10); T* p = &v; for (size_t n=0; n < v.size(); n++) assert( p+n == &v ); ...
4
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.