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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:04:45+00:00 2026-06-04T14:04:45+00:00

I have a list I need to process. The items are either enabled or

  • 0

I have a list I need to process. The items are either enabled or disabled. The user can choose whether or not to show disabled items.

So you have cond2 that depends on the items, and cond1 that does not. Here’s the dilemma I got into: Should I use cond1 && !cond2 or !(!cond1 || cond2)? Or should I check for the cond2 (show disabled items) before the loop? I also thought (as you will see in the code I put) if I should put the cond2 before cond1, because cond2
is a boolean variable, and with “short-circuits” (lazy evaluation?), it will be faster?

My main concern was speed. If I have many items in the loop, this might be an important change.

This is code that illustrates the options:

// First Option
for (String item : items) {
    doSomethingFirst(item);
    if (isDisabled(item) && !showDisabled) {
        continue;
    }
    doSomethingElse(item);
}

// Second Option
for (String item : items) {
    doSomethingFirst(item);
    if (!(!isDisabled(item) || showDisabled)) {
        continue;
    }
    doSomethingElse(item);
}

// Third Option
if (showDisabled) {
    for (String item : items) {
        doSomethingFirst(item);
        doSomethingElse(item);
    }
} else {
    for (String item : items) {
        doSomethingFirst(item);
        if (isDisabled(item)) {
            continue;
        }
        doSomethingElse(item);
    }
}

So, does the order of isDisabled(item) and showDisabled matter? Should I be checking on things before the loop? Or does the compiler optimize that? (I doubt…)

PS I don’t know how I would take measurements to see actual values, if it’s relevant please do.

Thanks.

  • 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-04T14:04:47+00:00Added an answer on June 4, 2026 at 2:04 pm

    In Java the expression is evaluated from left to right. With && if the first condition is false, the second one is not executed, with || if the first condition is true, the second one is not executed.

    So try to put the condition that can be resolve faster in first place, in your case showDisabled.

    For the third example, it looks better because you check the boolean only once but I guess it don’t really change performance, a bool comparison is not really costly. You will probably have better improvement to do in other part of your code. (and for the readable aspect it’s not my favorite – quite long)

    If you want to measure the performance in your case use a profiler for example.

    Or add in your code :

    long start=System.currentTimeMillis();
    //code to analyse
    long timeSpent = System.currentTimeMillis()-start
    

    You’ll have to put your code in a loop, to make it relevant. And you will probably notice that Java will increase the performance after some loops ;).

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

Sidebar

Related Questions

I have a list of strings that I need to pass to a process
I have a list of work items that need to be processed in order.
I have a problem in that I need to process a list of numbers,
I have a list that I need to send through a URL to a
I have list of catalog paths and need to filter out some of them.
I have List[(String,String)] and I need to sort them by the 2nd value and
so i have a list where i need to add new values constantly but
I need help with CMD scripts. Here is my problem: I have list of
I have a list of callback functions that I need to invoke when an
I have a list of xml elements that i need to search for specific

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.