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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:35:13+00:00 2026-06-05T03:35:13+00:00

Please compare two ways of setting/returning an array: static public float[] test_arr_speeds_1( int a

  • 0

Please compare two ways of setting/returning an array:

static public float[] test_arr_speeds_1( int a ) {
  return new float[]{ a, a + 1, a + 2, a + 3, a + 4, a + 5,
                      a + 6, a + 7, a + 8, a + 9 };
} // or e.g. field = new float... in method

static public float[] test_arr_speeds_2( int a ) {
  float[] ret = new float[10];
  ret[0] = a;
  ret[1] = a + 1;
  ret[2] = a + 2;
  ret[3] = a + 3;
  ret[4] = a + 4;
  ret[5] = a + 5;
  ret[6] = a + 6;
  ret[7] = a + 7;
  ret[8] = a + 8;
  ret[9] = a + 9;
  return ret;
} // or e.g. field[0] = ... in method

Both generate distinct bytecodes and both can be decompiled to their former state. After checking the execution times via profiler (100M iterations, unbiased, different environs), the time of _1 method is approx. 4/3 the time of _2, even though both create a new array and both set every field to a given value. The times are negligible most of the time, but this still bugs me – why is _1 visibly slower? Can anybody check/confirm/explain it to me in a reasonable, JVM-supported way?

  • 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-05T03:35:14+00:00Added an answer on June 5, 2026 at 3:35 am

    Here is the difference between bytecode (only for first two items). First method:

    bipush  10
    newarray float      //creating an array with reference on operand stack
    
    dup
    iconst_0
    iload_0
    i2f
    fastore             //setting first element
    
    dup
    iconst_1
    iload_0
    iconst_1
    iadd
    i2f
    fastore             //setting second element
    
    //...
    areturn             //returning the top of the operand stack
    

    Second method:

    bipush  10
    newarray float
    astore_1            //creating an array and storing it in local variable
    
    aload_1
    iconst_0
    iload_0
    i2f
    fastore             //setting first element
    
    aload_1
    iconst_1
    iload_0
    iconst_1
    iadd
    i2f
    fastore             //setting second element
    
    //...
    aload_1
    areturn
    

    As you can see the only difference is that the array reference is kept on operand stack in the first scenario (that’s why dup appears so many times – to avoid loosing a reference to an array after fastore) while in the second scenario the array reference is kept on normal stack (where method arguments and local variables are kept). In this scenario the reference must be read all the time (aload_1) because fastore requires arrayref to be on on the operand stack.

    We shouldn’t make assumptions based on this bytecode – after all it is translated to CPU instructions by jit and most likely in both cases array reference is stored in one of the CPU registers. Otherwise the performance difference would be huge.

    If you can measure the difference and you are doing so low-level optimizations – pick the version that is faster. But I doubt the difference is “portable” (depending on the architecture and JVM version/implementation you will observer different timing behaviour). That being said – I would go for more readable version, rather than the one that happens to be faster on your computer.

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

Sidebar

Related Questions

Can somebody please compare these two logging mechanism?
This is the Javascript Function. Please Explain How to compare two string in Javascript.
I am trying to build a timer. Please compare the two situations (the first
In excel 2010, I have to compare two versions (old, new) of the same
Please compare these two codes. I can't understand why former one didn't work while
I wrote a template function to compare two variables: template <class t> int compare(const
How can compare two dates like this format var CurrDate = new Date().format(MM/dd/yyyy); if
Possible Duplicate: PHP compare array I have to compare two arrays in php and
Would anyone please be able to tell me the most efficient way to compare
Please see following examples first, and which one is better? Could you compare some

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.