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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:14:57+00:00 2026-06-09T19:14:57+00:00

Somebody asked me a question: Which one is the fastest among the below two

  • 0

Somebody asked me a question:
Which one is the fastest among the below two scenario:

Case 1: assume int count = 0;

for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 5; j++)
    {
        count++;
    }
}

Case 2: assume int count = 0;

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 10; j++)
    {
        count++;
    }
}

In both the scenario’s the final valuse of count will be 50.
But I am not sure which one will be faster? I think CASE II is faster but not sure…

It will be great if anyone can throw some light on it. which is faster and why?

  • 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-09T19:14:59+00:00Added an answer on June 9, 2026 at 7:14 pm

    this is the only example i can think of where it matters which variable you iterate where

    int array[n][m];
    //fast
    for (int i=0;i<n;i++)
    { for(int j=0;j<m;j++)
      { count+=array[i][j];
      }
    }
    //slow
    for (int i=0;i<m;i++)
    { for(int j=0;j<n;j++)
      { count+=array[j][i];
      }
    }
    

    the second one is slower because you don’t iterate the locations in memory one after the other, but because you jump by m locations at a time. the processor caches the memory locations positioned immediately after an accessed location.

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

Sidebar

Related Questions

Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
There is one aspect of TDD which I never fully understood. Suppose somebody asked
Somebody asked me this question: Two processes P1 and P2 are using a shared
i recently asked question which was closed down linked here. https://stackoverflow.com/questions/3827349/how-to-use-one-openid-across-multiple-domains-closed I did not
somebody mentioned that the function (in this case a method) below is no good
I have looked around here to see if somebody has asked this question before
Good afternoon, Somebody asked me a question today and neither did I know the
Somebody has already asked my question about detecting SVG support in browsers but there
Somebody advised me to use Entity Framework here when I asked this question: what
I did a search for this question thinking that somebody must have asked it

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.