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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:34:41+00:00 2026-05-13T17:34:41+00:00

Lets consider two almost identical codes: First for (int k=0;k<1000;k++) { for (int i=0;i<600;i++)

  • 0

Lets consider two almost identical codes:

First

for (int k=0;k<1000;k++)
{
    for (int i=0;i<600;i++)
    {
         for (int j=0;j<600;j++)
         {
              tab[i][j] = i *j;
         }
    }
 }

Second

for (int k=0;k<1000;k++)
{
    for (int i=0;i<600;i++)
    {
         for (int j=0;j<600;j++)
         {
              tab[j][i] = i *j;
         }
    }
 }

In the second one instead of tab[i][j] we have tab[j][i].
The first code is much faster.

Question
Why is the first code much faster?

My intuition
Is it because when a program tries to access a cell , firstly whole block that contains this cell is move to the cache, and then it is accessed via cache. Since array in memory is represented by consecutive cells, then in first case then in first case there is much less access to the memory than in the second case.

  • 1 1 Answer
  • 3 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-13T17:34:41+00:00Added an answer on May 13, 2026 at 5:34 pm

    That’s because of cache locality. Processor cache line can hold several array elements at once but only from the adjacents addresses.

    In the first case you have more cache hits – when you iterate over the second array index you access adjacent elements. You access some element, the processor loads it and its neighbours into the cache line, the next adjacent accesses produce cache hits – you no longer need to access memory to deal with them.

    In the second case when you iterate over the first index you load some element, a line of cache gets filled, but the next access is to an element that is not in the same line. Thie makes the processor load yet another line into the cache. If the cache can’t hold all lines at once it has to discard previously loaded lines and reload them later. This greatly increases number of memory accesses and thus increases execution time

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

Sidebar

Related Questions

Lets consider following two codes First: for (int i=0;i<10000000;i++) { char* tab = new
Let's consider two tables: First: Id Data 1 asd 2 buu And Second: UPD:
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Let's consider a simple mapping example: val a = Array(One, Two, Three) val b
I have a question about HTTPService and the data it returns. Well lets consider
Consider these two snippets: try: a+a=a except SyntaxError: print first exception caught . try:
Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'
For example lets consider the following 2 codes: for (i = 0; i <
Lets consider the following linq statement. var ctx = new MoviesContext(); var movies =
For my question lets consider the following sample table data: ProductID    ProductName    Price   Category 1                Apple                 5.00       Fruits

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.