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

The Archive Base Latest Questions

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

Which function is faster (p is atomic public int property of MyObject): public static

  • 0

Which function is faster (p is atomic public int property of MyObject):

public static boolean check(MyObject o1, List<MyObject> list) {

    int p = o1.p;
    for (int i = 0; i < 1000000; i++) {
        MyObject o = list.get(i);
        if (o.p < p) return false;
    }

    return true;
}

or

public static boolean check(MyObject o1, List<MyObject> list) {

    for (int i = 0; i < 1000000; i++) {
        MyObject o = list.get(i);
        if (o.p < o1.p) return false;
    }

    return true;
}

So by using the local variable p we cache the object property call or it is done inline by the compiler optimization?

  • 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:03:37+00:00Added an answer on June 9, 2026 at 7:03 pm

    Short Answer: It depends

    Slightly longer Answer: It depends on the compiler, the VM and the settings of your VM.

    Background: Using the HotSpot VM (most common flavor) in server mode will make both variants equal since the VM does loop invariant hoisting in server mode. In client mode this may be done, may not be done or even may be done later if the VM considers it worthy of optimisations.

    Loop invariant hoisting is one of the loop optimisation, that is implemented into most modern compilers (or in the case of Java, VMs). As for the code generated by javac: Without further optimization done by the VM your first code sniplet will perfom faster.

    public static boolean check(Test$MyObject, java.util.List);
      Code:
       0:   aload_0
       1:   getfield        #7; //Field Test$MyObject.p:I
       4:   istore_2
       5:   iconst_0
       6:   istore_3
       7:   iload_3
       8:   ldc     #4; //int 1000000
       10:  if_icmpge       42
       13:  aload_1
       14:  iload_3
       15:  invokeinterface #11,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       20:  checkcast       #5; //class Test$MyObject
       23:  astore  4
       25:  aload   4
       27:  getfield        #7; //Field Test$MyObject.p:I
       30:  iload_2
       31:  if_icmpge       36
       34:  iconst_0
       35:  ireturn
       36:  iinc    3, 1
       39:  goto    7
       42:  iconst_1
       43:  ireturn
    

    —

    public static boolean check(Test$MyObject, java.util.List);
      Code:
       0:   iconst_0
       1:   istore_2
       2:   iload_2
       3:   ldc     #4; //int 1000000
       5:   if_icmpge       38
       8:   aload_1
       9:   iload_2
       10:  invokeinterface #11,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       15:  checkcast       #5; //class Test$MyObject
       18:  astore_3
       19:  aload_3
       20:  getfield        #7; //Field Test$MyObject.p:I
       23:  aload_0
       24:  getfield        #7; //Field Test$MyObject.p:I
       27:  if_icmpge       32
       30:  iconst_0
       31:  ireturn
       32:  iinc    2, 1
       35:  goto    2
       38:  iconst_1
       39:  ireturn
    

    As you can see, the getfield operation from line 20 in the second example lies in line 1 in the first and is outside of the loop (lines 7 to 39 in variant 1 and lines 2 to 35 in variant 2) and is therefore only executed ondced instead of 1000000 times.

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

Sidebar

Related Questions

Just curious about which of these would be faster? $('ul.dropdown a').first().click(function(event) { event.stopPropagation(); return
which function windows is calling to list the files in a directory ? thanks
Which function can I use to get the content of a plain text file?
I have a function which takes several boolean template arguments: template<bool par1, bool par2,
For doing insensitive case search in strings which function is faster preg_match() or stripos()?
Which one of these two ways is faster and why? window.setTimeout(func(), 100); Or window.setTimeout(function(){func();},
I am just wondering which will be faster t-sql function/procedure or clr version of
Context I have this function which synchronize a list of sites between a table
Which function grows faster :lg( √n ) vs. √ lg n? When I did
Which function grows faster, exponential (like 2^n, n^n, e^n etc) or factorial (n!)? Ps:

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.