efficiency of std::vector for small arrays.

C

Carter

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.
 
G

Greg Herlihy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top