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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:12:54+00:00 2026-05-12T01:12:54+00:00

I got one problem at my exam for subject Principal of Programming Language. I

  • 0

I got one problem at my exam for subject Principal of Programming Language. I thought for long time but i still did not understand the problem

Problem:
Below is a program C, that is executed in MSVC++ 6.0 environment on a PC with configuration ~ CPU Intel 1.8GHz, Ram 512MB

#define M 10000
#define N 5000
int a[M][N];

void main() {
    int i, j;
    time_t start, stop;

    // Part A
    start = time(0);
    for (i = 0; i < M; i++)
        for (j = 0; j < N; j++)
            a[i][j] = 0;
    stop = time(0);
    printf("%d\n", stop - start);

    // Part B
    start = time(0);
    for (j = 0; j < N; j++)
        for (i = 0; i < M; i++)
            a[i][j] = 0;
    stop = time(0);
    printf("%d\n", stop - start);
}

Explain why does part A only execute in 1s, but it took part B 8s to finish?

  • 1 1 Answer
  • 2 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-12T01:12:54+00:00Added an answer on May 12, 2026 at 1:12 am

    Row-major order versus column-major order.

    Recall first that all multi-dimensional arrays are represented in memory as a continguous block of memory. Thus the multidimensional array A(m,n) might be represented in memory as

    a00 a01 a02 … a0n a10 a11 a12 … a1n a20 … amn

    In the first loop, you run through this block of memory sequentially. Thus, you run through the array traversing the elements in the following order

    a00  a01  a02  ...  a0n  a10  a11  a12  ...  a1n  a20 ... amn
    
    1    2    3         n    n+1  n+2  n+3 ...   2n   2n+1    mn
    

    In the second loop, you skip around in memory and run through the array traversing the elements in the following order

    a00  a10  a20  ...  am0  a01  a11  a21  ...  am1  a02  ...  amn
    

    or, perhaps more clearly,

    a00  a01  a02  ...  a10  a11  a12  ...  a20 ... amn
    1    m+1  2m+1      2    m+2  2m+2      3       mn
    

    All that skipping around really hurts you because you don’t gain advantages from caching. When you run through the array sequentially, neighboring elements are loaded into the cache. When you skip around through the array, you don’t get these benefits and instead keep getting cache misses harming performance.

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

Sidebar

Related Questions

I have got one problem with the UITableViewController... The viewWillAppear method is not getting
I'm new to Twig and like it, but i've got one problem right now.
I got my own custom font working in Java, but I have one problem,
I'm programming both in JAVA and PHP. I got one problem with string comparison.
I have written a forum, but I've got one problem... The text from the
I am creating a registration form, but I have got one problem only... why
I'm working on a fairly simple console application but I got one little problem.
I'm skinning scrollbar in my flex app and got one problem. This white square
I've got one head scratcher of a problem that I can't seem to find
I got a simple problem in SQLAlchemy. I have one model in a table,

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.