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 7793667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:33:36+00:00 2026-06-01T22:33:36+00:00

I have a nested for-loop structure and right now I am re-declaring the vector

  • 0

I have a nested for-loop structure and right now I am re-declaring the vector at the start of each iteration:

void function (n1,n2,bound,etc){

    for (int i=0; i<bound; i++){
             vector< vector<long long> > vec(n1, vector<long long>(n2));
             //about three more for-loops here
    }
}

This allows me to “start fresh” each iteration, which works great because my internal operations are largely in the form of vec[a][b] += some value. But I worry that it’s slow for large n1 or large n2. I don’t know the underlying architecture of vectors/arrays/etc so I am not sure what the fastest way is to handle this situation. Should I use an array instead? Should I clear it differently? Should I handle the logic differently altogether?

EDIT: The vector’s size technically does not change each iteration (but it may change based on function parameters). I’m simply trying to clear it/etc so the program is as fast as humanly possible given all other circumstances.

EDIT:

My results of different methods:

Timings (for a sample set of data):
reclaring vector method: 111623 ms
clearing/resizing method: 126451 ms
looping/setting to 0 method: 88686 ms
  • 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-06-01T22:33:38+00:00Added an answer on June 1, 2026 at 10:33 pm

    Here is some code that tests a few different methods.

    #include <chrono>
    #include <iostream>
    #include <vector>
    
    int main()
    {
      typedef std::chrono::high_resolution_clock clock;
    
      unsigned n1 = 1000;
      unsigned n2 = 1000;
    
      // Original method
      {
        auto start = clock::now();
        for (unsigned i = 0; i < 10000; ++i)
        {
          std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
          // vec is initialized to zero already
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    
    
      // reinitialize values to zero at every pass in the loop
      {
        auto start = clock::now();
        std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
        for (unsigned i = 0; i < 10000; ++i)
        {
          // initialize vec to zero at the start of every loop
          for (unsigned j = 0; j < n1; ++j)
            for (unsigned k = 0; k < n2; ++k)
                vec[j][k] = 0;
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    
      // clearing the vector this way is not optimal since it will destruct the
      // inner vectors
      {
        auto start = clock::now();
        std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
        for (unsigned i = 0; i < 10000; ++i)
        {
          vec.clear();
          vec.resize(n1, std::vector<long long>(n2));
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    
      // equivalent to the second method from above
      // no performace penalty
      {
        auto start = clock::now();
        std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
        for (unsigned i = 0; i < 10000; ++i)
        {
          for (unsigned j = 0; j < n1; ++j)
          {
            vec[j].clear();
            vec[j].resize(n2);
          }
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    }
    

    Edit: I’ve updated the code to make a fairer comparison between the methods.
    Edit 2: Cleaned up the code a bit, methods 2 or 4 are the way to go.

    Here are the timings of the above four methods on my computer:

    16327389
    15216024
    16371469
    15279471
    

    The point is that you should try out different methods and profile your code.

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

Sidebar

Related Questions

I have a nested loop structure in my code. Sometimes, I get the below
I have two nested loop in XSL like this, at this moment I use
I have the following data and nested for loop: x <- c(12, 27, 21,
I have the following nested for loops. I want to parallelize the first loop
I have nested iframes and I would like to use onload function for the
I have this nested loop that goes 4 levels deep to find all the
I have a nested loop that I'm using foreach, DoSNOW, and a SNOW socket
I have a nested loop that I would like to break out of. After
If I have a nested loop scenario and I am stepping into it in
I have a nested while loop inside a foreach loop where I would like

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.