Std :: vector <std :: vector <unsigned char >> or std :: deque <std :: vector <unsigned char >>?
I have an existing algorithm, and I need to optimize it if possible. Modifying this algorithm is not currently an option. The algorithm works with the instance std::vector< std::vector<unsigned char> >. It looks like this:
typedef std::vector<unsigned char> internal_vector_t;
std::vector< internal_vector_t > internal_vectors;
while (fetching lots of records) {
internal_vector_t tmp;
// reads 1Mb of chars in tmp...
internal_vectors.push_back(tmp);
// some more work
}
// use this internal_vectors
The algorithm inserts many times into internal_vectorsinstances of internal_vector_t using push_back (). Most instances of inner_vector_t are 1 MB in size . Since the size is internal_vectorsunknown, reserve () is not executed in advance.
, , , , internal_vectors , . 1 , - . , (gcc 4.3, MS V++ 2008) , ?
std::deque help? std:: deque, , internal_vectors [10]. :
typedef std::vector<unsigned char> internal_vector_t;
std::deque< internal_vector_t > internal_vectors;
// the same while
, std::deque , - . , std::deque push_backs?
</" > Update:
1) DeadMG MSVC9 (The Swaptimization - TR1 Fixes In VC9 SP1). gcc 4.3, , .
2) , std::deque< std::vector<unsigned char> >, , .
3) swap, Mark Ransom. :
internal_vector_t tmp;
internal_vectors.push_back(empty);
tmp.swap(internal_vectors.back());
, internal_vector_t internal_vectors, internal_vector_t. , vector deque. , .
, internal_vector_t, swap , .
, , . Deque , .
: , , . .
typedef std::vector<unsigned char> internal_vector_t;
std::deque< internal_vector_t > internal_vectors;
internal_vector_t empty;
while (fetching lots of records) {
internal_vector_t tmp;
// reads 1Mb of chars in tmp...
internal_vectors.push_back(empty);
tmp.swap(internal_vectors.back());
// some more work
}
Failure may be more effective depending on the implementation. Unlike a vector, dequeue does not guarantee continuous storage and therefore can allocate several separate memory blocks. Therefore, it can allocate more memory without adding roaming elements. You must try and measure the impact.