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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:24:01+00:00 2026-06-08T16:24:01+00:00

I created a very simple code to learn if vector access is faster than

  • 0

I created a very simple code to learn if vector access is faster than matrix access.

I tried 3 things:

1: Create a vector with 100.000.000 elements of int:

int *matrix=(int*)malloc(sizeof(int)*100000*1000)
for(long int=x;x<100000*1000;x++)matrix[x]=1;

2: Create a matrix with the same size:

int ** matrix=(int**)malloc(sizeof(int*)*100000);
for(long int=0; x<100000;x++){
   matrix[x]=(int*)malloc(sizeof(int*)*1000);
}
for(int x=0; x<100000;x++){
   for(int y=0;y<1000;y++){
     matrix[x][y]=1;
   }
}

3: Create the same vector, but write inside it as matrix

for(int x=0; x<100000;x++){
   for(int y=0;y<1000;y++){
     matrix[(x*1000)+y]=1;
   }
}

Always the matrix access (CASE 2) takes 2 times the case 1 and 3.The Case 3 is a little bit faster then Case 1.
I’m using the -O2 param in my C++ compiler (g++)

I can understand why the vector is faster then the matrix: (But i’ll love some explanation).
But i cannot understand why the Case 3 is faster than the case 1, i imagined that the multiplication process will slow down the things a lot and not make it faster.
I don’t understand why, even if the difference is 0.002 ( It could be the time and the processor usage in the time (i imagine))

If i compile all the 3 cases without optimization the Case 2 is the slower, case 3 than case 1.
So, without the optimization process the case 1 is faster.

Vector is, usually, faster?

Thanks

  • 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-08T16:24:04+00:00Added an answer on June 8, 2026 at 4:24 pm

    The reason why case 2 is the slowest, is because it has one more level of indirection.

    In case 1 and 3 you fetch the desired element from memory. While in case 2 you first have to fetch the address of the row/col array from memory, to make afterwards the fetch from the desired element. As in modern computer the memory accesses are the far most expensive operations (in terms of execution), it is no wonder that it is much slower.

    The difference in 1 and 3 is as expected very minimal. Fiddling with the optimizing options already makes a difference, so here nobody can give you a definitivly answer with knowing the exact machine you are using. Best (and only reasonable) approach here would be, to look at the generated assembler code. One reason could be that in one version your loop variable is long, in the other not (therefor you do there the element address computation), depending on your cpu this can make a difference.

    EDIT: Your wording is choosen very bad, as there is no Matrix memory access. Memory is always flat. A matrix addressing is just an “virtual” addressing you put on top (either directly like you did e.g. in 3) or indirect (by using e.g. a different language which does it for you, like fortran). So you have more or less to distinguish between different memory layouts for matrices. In 3 you have the matrix as one big chunk in the matrix, while in 2 you have it row-by-row/col-by-col in the memory, which has the disadvantage that it one more level of indirection (but has the advantage that you can faster perform certain operations, like e.g. swapping to rows, and that it can be better for a garbage collector). There are also many other ways to store matrices in memory (esp. if you have to handle sparse matrices).

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

Sidebar

Related Questions

I have created a very simple app to invite friends. This is my code:
The following simple code pattern is very common in graphics programming. It creates an
I have created very simple app with persistence context (hibernate as provider) to read
I created two very simple Heroku apps to test out the service, but it's
What I have created a very simple asp.net web service using .NET framework 3.5,
I've created a very simple Enterprise Application project with about 7 entity beans and
I've created a very simple drop down list in excel. It holds a sequence
I have created a very simple sharepoint timer job. All i want it to
I have created a very simple GUI project in Qt as follows: main: #include
Ok, so i created a very simple splash screen using this tutorial: http://p-xr.com/android-tutorial-how-to-make-a-basic-splash-screen/ The

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.