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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:47:34+00:00 2026-05-24T00:47:34+00:00

How inefficient is that: List<Object> getList(){ return new LinkedList<Object>(); } void foo(){ for(Object o:

  • 0

How inefficient is that:

List<Object> getList(){
    return new LinkedList<Object>();
}

void foo(){
    for(Object o: getList()){
       do something with o;
    }
}

in compare with that:

List<Object> getList(){
    return new LinkedList<Object>();
}

void foo(){
    List<Object>  os = getList();
    for(Object o: os){
       do something with o;
    }
}
  • 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-05-24T00:47:35+00:00Added an answer on May 24, 2026 at 12:47 am

    There won’t be any perceptible difference.

    I’ve compiled the following code:

    import java.util.LinkedList;
    import java.util.List;
    
    public class Test1 {
    
        static List<Object> getList(){
            return new LinkedList<Object>();
        }
    
        void process(Object o) {}
    
        void foo1(){
            for(Object o: getList()){
                process(o);
            }
        }
    
        void foo2(){
            List<Object>  os = getList();
            for(Object o: os){
                process(o);
            }
        }
    }
    

    The bytecodes for foo1 and foo2 are as follows:

    void foo1();
      Code:
       0:   invokestatic    #26; //Method getList:()Ljava/util/List;
       3:   invokeinterface #28,  1; //InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
       8:   astore_2
       9:   goto    24
       12:  aload_2
       13:  invokeinterface #34,  1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
       18:  astore_1
       19:  aload_0
       20:  aload_1
       21:  invokevirtual   #40; //Method process:(Ljava/lang/Object;)V
       24:  aload_2
       25:  invokeinterface #42,  1; //InterfaceMethod java/util/Iterator.hasNext:()Z
       30:  ifne    12
       33:  return
    
    void foo2();
      Code:
       0:   invokestatic    #26; //Method getList:()Ljava/util/List;
       3:   astore_1
       4:   aload_1
       5:   invokeinterface #28,  1; //InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
       10:  astore_3
       11:  goto    26
       14:  aload_3
       15:  invokeinterface #34,  1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
       20:  astore_2
       21:  aload_0
       22:  aload_2
       23:  invokevirtual   #40; //Method process:(Ljava/lang/Object;)V
       26:  aload_3
       27:  invokeinterface #42,  1; //InterfaceMethod java/util/Iterator.hasNext:()Z
       32:  ifne    14
       35:  return
    

    As you can see for yourself, the bytecodes for the two loops are identical. The only difference is that foo2 stores and loads the list reference in a local variable at the start.

    One might argue that a better optimizing compiler may be able to eliminate os altogether, producing identical code for both functions.

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

Sidebar

Related Questions

In short, I have an object called PersonList that has a List of Person
I read somewhere once that the modulus operator is inefficient on small embedded devices
I just jumped on a feature written by someone else that seems slightly inefficient,
Is there an analog of setattr() that allows for appending an arbitrary list property
I've got some code on my site that displays a list of articles on
I have a long list of links that I spit out using the below
I am new to Python - be forewarned! I have a template class that
Of what I know of benefits of using initialization list is that they provide
My app has a DataGridView object and a List of type MousePos. MousePos is
I have a list of elements around 1000. Each element (objects that i read

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.