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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:51:37+00:00 2026-05-10T18:51:37+00:00

Consider the following two ways of writing a loop in Java to see if

  • 0

Consider the following two ways of writing a loop in Java to see if a list contains a given value:

Style 1

boolean found = false; for(int i = 0; i < list.length && !found; i++) {    if(list[i] == testVal)      found = true; } 

Style 2

boolean found = false; for(int i = 0; i < list.length && !found; i++) {    found = (list[i] == testVal); } 

The two are equivalent, but I always use style 1 because 1) I find it more readable, and 2) I am assuming that reassigning found to false hundreds of times feels like it would take more time. I am wondering: is this second assumption true?

Nitpicker’s corner

  • I am well aware that this is a case of premature optimization. That doesn’t mean that it isn’t something that is useful to know.
  • I don’t care which style you think is more readable. I am only interested in whether one has a performance penalty compared to the other.
  • I know that style 1 has the advantage of allowing you to also put a break; statement in the if block, but I don’t care. Again, this question is about performance, not style.
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T18:51:37+00:00Added an answer on May 10, 2026 at 6:51 pm

    Well, just write a micro benchmark:

     import java.util.*;  public class Test {     private static int[] list = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9} ;     private static int testVal = 6;       public static boolean version1() {         boolean found = false;         for(int i = 0; i < list.length && !found; i++)         {         if(list[i] == testVal)             found = true;         }         return found;      }      public static boolean version2() {     boolean found = false;     for(int i = 0; i < list.length && !found; i++)         {         found = (list[i] == testVal);         }      return found;     }       public static void main(String[] args) {          // warm up     for (int i=0; i<100000000; i++) {         version1();         version2();     }       long time = System.currentTimeMillis();     for (int i=0; i<100000000; i++) {         version1();     }      System.out.println('Version1:' + (System.currentTimeMillis() - time));      time = System.currentTimeMillis();     for (int i=0; i@lt;100000000; i++) {         version2();     }          System.out.println('Version2:' + (System.currentTimeMillis() - time));     } } 

    On my machine version1 seems to be a little bit faster:

    Version1:5236

    Version2:5477

    (But that’s 0.2 seconds on a 100 million iterations. I wouldn’t care about this.)

    If you look at the generated bytecode there are two more instructions in version2 which probably cause the longer execution time:

     public static boolean version1();   Code:    0:   iconst_0    1:   istore_0    2:   iconst_0    3:   istore_1    4:   iload_1    5:   getstatic   #2; //Field list:[I    8:   arraylength    9:   if_icmpge   35    12:  iload_0    13:  ifne    35    16:  getstatic   #2; //Field list:[I    19:  iload_1    20:  iaload    21:  getstatic   #3; //Field testVal:I    24:  if_icmpne   29    27:  iconst_1    28:  istore_0    29:  iinc    1, 1    32:  goto    4    35:  iload_0    36:  ireturn  public static boolean version2();   Code:    0:   iconst_0    1:   istore_0    2:   iconst_0    3:   istore_1    4:   iload_1    5:   getstatic   #2; //Field list:[I    8:   arraylength    9:   if_icmpge   39    12:  iload_0    13:  ifne    39    16:  getstatic   #2; //Field list:[I    19:  iload_1    20:  iaload    21:  getstatic   #3; //Field testVal:I    24:  if_icmpne   31    27:  iconst_1    28:  goto    32    31:  iconst_0    32:  istore_0    33:  iinc    1, 1    36:  goto    4    39:  iload_0    40:  ireturn 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following two Java files that contain a simplified version of my issue
consider the following two pieces of code: public static Time Parse(string value) { string
Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
Consider the following two blocks of Java psedo-code in a system that uses optimistic
Consider the following two structures: template <typename T> struct duplet{ QString str; T value;
Please consider the following two initialization methods. The first method simply passes the value
Consider the following declarations: Object *a; Object *b; There are two ways of assigning
Given a $node , I'm trying to decide between the following two ways of
Consider the following two toolbars that are in the same project of mine: Notice
Consider the following two snippets: Exhibit A : template<typename CalcFuncT> int perform_calc(CalcFuncT&& calcfunc) {

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.