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

  • SEARCH
  • Home
  • 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 8784451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:05:31+00:00 2026-06-13T21:05:31+00:00

I have a really very basic doubt regarding STL containers. My requirement is that

  • 0

I have a really very basic doubt regarding STL containers.
My requirement is that i want to store double values in the form of multi-dimensional array. I will be performing various algebraic operations directly on them i.e.

myvector[4] = myvector[3] - 2 * myvector[2];

for this I am itterating using for loops & using the [] operator. I am not using STL itterator’s. I found 2 basic approaches here.
I prefer speed over memory efficiency. Since I am accessing these variables frequently I think vector would be slow for me.
So what is your humble opinion on this matter?
I know that the answers would be based on your previous experience, that is why I am asking this question. I am sorry if this question is too basic to be discussed here.

  • 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-13T21:05:33+00:00Added an answer on June 13, 2026 at 9:05 pm

    The link you gave listed 2 methods, which creates “real” 2d arrays. In general, 2d arrays are not that efficient, because they require a lot of allocations. Instead, you can use a faked 2d array:

    // Array of length L and width W
    type* array1 = new type[L * W]; // raw pointers
    std::vector<type> array2(L * W); // STL Vector
    
    // Accessing a value. You have to use a convention for indices, and follow it.
    // Here the convention is: lines are contiguous (index = x + y * W)
    type value = array[x + y * W]; // raw pointer array & vector
    

    Here is a simple benchmark (windows only, except if you change the timer part):

    #include <vector>
    #include <ctime>
    #include <iostream>
    #include <stdlib.h>
    
    #include <Windows.h>
    typedef LARGE_INTEGER clock_int;
    
    void start_timer(clock_int& v)
    {
        QueryPerformanceCounter(&v);
    }
    
    void end_timer(clock_int v, const char* str)
    {
        clock_int e;
        QueryPerformanceCounter(&e);
        clock_int freq;
        QueryPerformanceFrequency(&freq);
        std::cout << str << 1000.0 * ((double)(e.QuadPart-v.QuadPart) / freq.QuadPart) << " ms\n";
    }
    
    void test_2d_vector(unsigned int w, unsigned int h)
    {
        std::vector<std::vector<double> > a;
        a.resize(h);
        for(unsigned int t = 0; t < h; t++)
            a[t].resize(w);
    
        clock_int clock;
        start_timer(clock);
        // Benchmark random write access
        for(unsigned int t = 0; t < w * h; t++)
            a[rand() % h][rand() % w] = 0.0f;
        end_timer(clock,"[2D] Random write (STL) : ");
    
        start_timer(clock);
        // Benchmark contiguous write access
        for(unsigned int y = 0; y < h; y++)
            for(unsigned int x = 0; x < w; x++)
                a[y][x] = 0.0f;
        end_timer(clock,"[2D] Contiguous write (STL) : ");
    }
    
    void test_2d_raw(unsigned int w, unsigned int h)
    {
        double** a = new double*[h];
        for(unsigned int t = 0; t < h; t++)
            a[t] = new double[w];
    
        clock_int clock;
        start_timer(clock);
        // Benchmark random write access
        for(unsigned int t = 0; t < w * h; t++)
            a[rand() % h][rand() % w] = 0.0f;
        end_timer(clock,"[2D] Random write (RAW) : ");
    
        start_timer(clock);
        // Benchmark contiguous write access
        for(unsigned int y = 0; y < h; y++)
            for(unsigned int x = 0; x < w; x++)
                a[y][x] = 0.0f;
        end_timer(clock,"[2D] Contiguous write (RAW) : ");
    }
    
    void test_1d_raw(unsigned int w, unsigned int h)
    {
        double* a = new double[h * w];
    
        clock_int clock;
        start_timer(clock);
        // Benchmark random write access
        for(unsigned int t = 0; t < w * h; t++)
            a[(rand() % h) * w + (rand() % w)] = 0.0f;
        end_timer(clock,"[1D] Random write (RAW) : ");
    
        start_timer(clock);
        // Benchmark contiguous write access
        for(unsigned int y = 0; y < h; y++)
            for(unsigned int x = 0; x < w; x++)
                a[x + y * w] = 0.0f;
        end_timer(clock,"[1D] Contiguous write (RAW) : ");
    }
    
    void test_1d_vector(unsigned int w, unsigned int h)
    {
        std::vector<double> a(h * w);
    
        clock_int clock;
        start_timer(clock);
        // Benchmark random write access
        for(unsigned int t = 0; t < w * h; t++)
            a[(rand() % h) * w + (rand() % w)] = 0.0f;
        end_timer(clock,"[1D] Random write (STL) : ");
    
        start_timer(clock);
        // Benchmark contiguous write access
        for(unsigned int y = 0; y < h; y++)
            for(unsigned int x = 0; x < w; x++)
                a[x + y * w] = 0.0f;
        end_timer(clock,"[1D] Contiguous write (STL) : ");
    }
    
    int main()
    {
        int w=1000,h=1000;
        test_2d_vector(w,h);
        test_2d_raw(w,h);
        test_1d_vector(w,h);
        test_1d_raw(w,h);
        system("pause");
        return 0;
    }
    

    Compiled with msvc2010, release /Ox /Ot, it outputs for me (Win7 x64, Intel Core i7 2600K):

    [2D] Random write (STL) : 32.3436 ms
    [2D] Contiguous write (STL) : 0.480035 ms
    [2D] Random write (RAW) : 32.3477 ms
    [2D] Contiguous write (RAW) : 0.688771 ms
    [1D] Random write (STL) : 32.1296 ms
    [1D] Contiguous write (STL) : 0.23534 ms
    [1D] Random write (RAW) : 32.883 ms
    [1D] Contiguous write (RAW) : 0.220138 ms
    

    You can see the STL is equivalent to raw pointers. But 1D is much faster than 2D.

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

Sidebar

Related Questions

I have really basic question. How can I get form id by input element
I'm developing a site with CakePHP but I have a very basic doubt: When
I have a very basic regular expression that I just can't figure out why
We have a very basic application (iOS/Android) done in Appcelerator that will receive a
Very basic question, but I have an error in my code that can only
I have a very basic table that consists of worker names and manager names:
I have a very basic slideshow cycling some div containers. This is my code:
I have a very weird problem.. I really do hope someone has an answer
In software companies I have seen it's really hard to work on very large
I have a very basic ASP.NET web site. It has a single page (TestPage.aspx)

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.