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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:58:58+00:00 2026-06-11T23:58:58+00:00

I often find my self writing these pieces of code, specifically when I have

  • 0

I often find my self writing these pieces of code, specifically when I have to do something in a 2D array.

The loops are the same, except the operations inside are different and, most importantly, the operation in the last group depends on the first.

My main concern is: is there a more efficient code for large values of n,m?

for ( int y = 0 ; y < m ; ++y ) {
  for ( int x = 0 ; x < n ; ++x ) {
    if ( v[x][y] == z ) a = true;
  }
}

for ( int y = 0 ; y < m ; ++y ) {
  for ( int x = 0 ; x < n ; ++x ) {
    if ( a == true ) do_something( v[x][y] );
  }
}

Thanks in advance

  • 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-11T23:59:00+00:00Added an answer on June 11, 2026 at 11:59 pm

    In the general case as you describe it, the answer is probably “no” – you imply that the operation of the second look relies on the first loop being completed, so you have to do just that.

    However, in the specific case you’ve listed, there are two easy optimisations:

    1. fail fast out of the first loop: once a is set true there’s no need to loop any further.
    2. move the if ( a == true ) outside of the second loop, so that it’s only evaluated once and you skip the entire loop if it’s false.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find myself doing inefficient loops as such: def __add__(self, other): dimensions =
I often find myself writing very similar code across projects. And more often than
I often find myself writing class constructors like this: class foo: def __init__(self, arg1,
I find myself writing this class often in my python code when I need
I often find myself writing one off queries to either answer someone's question or
I often find when I'm writing a service class (within an ASP.NET app) that
Often I find my self navigating the filesystem from a Conque shell in Vim
I often find myself writing R scripts that generate a lot of output. I
I often find myself wanting to change just something little in a colorscheme, but
I'm pretty new to WPF. I often find my self struggling with getting a

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.