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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:42:25+00:00 2026-05-16T23:42:25+00:00

Here is the code, int array[X][Y] = {0,}; // 1 way to access the

  • 0

Here is the code,

int array[X][Y] = {0,};

// 1 way to access the data
for (int x = 0; x < X; x++)
  for(int y = 0; y < Y; y++)
    array[x][y] = compute();

// the other way to access the data
for (int y = 0; y < Y; y++)
  for (int x = 0; x < X; x++)
    array[x][y] = compute();

Is it true that the first way is more efficient than the second one since the CPU cache(L1, L2?) optimization? In other words, whether sequential access pattern is preferred even for RAM?

  • 1 1 Answer
  • 1 View
  • 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-16T23:42:26+00:00Added an answer on May 16, 2026 at 11:42 pm

    You’ll understand this better if you draw a picture of your array in memory:

      Y ->
    X xxxxx ...
    | xxxxx
    v xxxxx
      .
      .
    

    The adress you access will grow linear in Y direction (345, 345+1, 345+2…), but jumps wildly in X direction if Y is large (345, 345+X, 345+X*2). As the cache loads blocks of memory, you’ll jump out of them very soon with Y big enough, but will always be in the cache page when walking in Y direction until the cache has to be refreshed.

    Also note that this effect can be more extreme when using dynamical allocation. Using the following program with full optimizations gives me the following output (times in seconds)

    0.615000
    9.878000
    

    EDIT: Other interesting measures:

    Replacing the array code with int array[X][Y]; will use stack memory which is limited, so you can’t test much bigger X/Y values, but also very fast:

    0.000000
    0.000000
    

    Using int array[X][Y]; as a global variable will use a block of heap memory and is slow again. So even without dynamical allocation, the first case is much better:

    0.929000
    8.944000
    

    Using X=1500, Y=1500 shows that the effect is measurable even with smaller arrays:

    0.008000
    0.059000
    

    EDIT2: Also note there are other possible optimizations to the code as jalf said in a comment to your question. Using this optimization indeed almost doubles the speed (0.453 seconds for X=Y=10000):

    // an even faster way to access the array
    for (int x = 0; x < X; x++) {
      int* arrayptr = array[x];
      for (int y = 0; y < Y; y++, arrayptr++)
        *arrayptr = x;
    }
    

    Code: (note you can also use this to measure your case where the difference shouldn’t be that extreme except for big X and Y. Like others already said, measure this and you’ll be enlightened).

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define X 10000
    #define Y 10000
    
    int main() {
    
      int** array = new int*[X];
    
      for (int x = 0; x < X; x++) {
        array[x] = new int[Y];
      }
    
      double c = clock();  
    
      // 1 way to access the data
      for (int x = 0; x < X; x++)
        for(int y = 0; y < Y; y++)
          array[x][y] = x;
    
      printf("%f\n", (clock() - c) / CLOCKS_PER_SEC);
    
      c = clock();  
    
      // the other way to access the data
      for (int y = 0; y < Y; y++)
        for (int x = 0; x < X; x++)
          array[x][y] = x;
    
      printf("%f\n", (clock() - c) / CLOCKS_PER_SEC);
    
      for (int x = 0; x < X; x++) {
        delete(array[x]);
      }
      delete(array);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, here is some code: int main() { int days[] = {1,2,3,4,5}; int
I'm trying to write a video using opencv's VideoWriter. Here's code: int main() {
I got an EXC_BAD_ACCESS in main() , here is my code: int main(int argc,
here is my code for(int i = 0; i < number ; i++) {
Here is code example class A{ int i; public: A(int i) : i(i) {}
here's the code: class Acount { int sum ; String owner ; //these seem
So here is my code: public MyClass (int y) { super(y,x,x); //some code }
a piece of code here jmp_buf mark; int Sub_Func() { int be_modify, jmpret; be_modify
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
Here is my JAXB class, @XmlRootElement public class Status { private int code; private

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.