469,160 Members | 1,877 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

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 1899
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steve | last post: by
6 posts views Thread by Jason Heyes | last post: by
24 posts views Thread by simon | last post: by
8 posts views Thread by Ross A. Finlayson | last post: by
2 posts views Thread by Priya Mishra | last post: by
4 posts views Thread by Bobrick | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.