Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:51:34+00:00 2026-05-30T09:51:34+00:00

I have an existing algorithm and I need to optimize it sligtly if it

  • 0

I have an existing algorithm and I need to optimize it sligtly if it is possible. Changing a lot in this algorithm is not an option at the moment. The algoritm works with instance of 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 a lot of times in internal_vectors instances of internal_vector_t using push_back(). Most of instances of internal_vector_t have size 1 Mb. Since the size of the internal_vectors is unknown no reserve() is done beforehand.

The first thing that I don’t understand is what is happening when internal_vectors reachs its current capacity, needs to allocate a new block and copy its current content in the bigger block of memory. Since most of the blocks are 1Mb in size copying is a long operation. Should I expect that a compiler (gcc 4.3, MS VC++ 2008) will manage to optimize it in order to avoid copying?

If copying is unavoidable will changing to std::deque help? I consider std::deque because I still need accessing by index like internal_vectors[10]. Like this:

typedef std::vector<unsigned char> internal_vector_t;
std::deque< internal_vector_t > internal_vectors; 
// the same while

As far as I understand std::deque does not need relocate that was once allocated. Am I right that std::deque in this situation will requere less allocation and copying on push_backs?


Update:
1) According to DeadMG MSVC9 does this type of optimization (The Swaptimization – TR1 Fixes In VC9 SP1). gcc 4.3 probably doesn’t do this type of optimization.

2) I have profiled the version of the algorithm that use std::deque< std::vector<unsigned char> > and I see that its performace is better.

3) I have also made use of using swap that was suggested by Mark Ransom. Using this improved the performance:

   internal_vector_t tmp;
   internal_vectors.push_back(empty);
   tmp.swap(internal_vectors.back());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:51:35+00:00Added an answer on May 30, 2026 at 9:51 am

    MSVC9 implements something known as “swaptimization” for it’s Standard containers. It’s a weaker version of move semantics. When the external vector is resized, it will not copy the internal vectors.

    However, you’d do best simply upgrading your compiler to MSVC10 or GCC (4.5, I think it is) which will give you move semantics, which makes such operations vastly more efficient. Of course, a std::deque is probably still the smarter container, but move semantics are performance-beneficial in many, many places.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have existing java code and need to create Design Document based on that.
For whatever reason, I have a lot of clients that have existing data that's
I have an existing application that is written in C++ for Windows. This application
I need a flood fill algorithm to fill existing raster shapes with various colors
I need to write a speech detection algorithm (not speech recognition). At first I
I have existing code that uses CMNewProfileSearch to find then iterate over the color
I have existing Linux shared object file (shared library) which has been stripped. I
I have quick question for you SQL gurus. I have existing tables without primary
I'm working on a project where the client wants to have existing pages dynamically
We have an existing WCF service that makes use of wsDualHttpBinding to enable callbacks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.