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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:53:30+00:00 2026-05-10T19:53:30+00:00

Is there any performance difference between the for loops on a primitive array? Assume:

  • 0

Is there any performance difference between the for loops on a primitive array?

Assume:

double[] doubleArray = new double[300000];   for (double var: doubleArray)     someComplexCalculation(var); 

or :

for ( int i = 0, y = doubleArray.length; i < y; i++)    someComplexCalculation(doubleArray[i]); 

Test result

I actually profiled it:

Total timeused for modern loop= 13269ms Total timeused for old loop   = 15370ms 

So the modern loop actually runs faster, at least on my Mac OSX JVM 1.5.

  • 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. 2026-05-10T19:53:31+00:00Added an answer on May 10, 2026 at 7:53 pm

    Your hand-written, ‘old’ form executes fewer instructions, and may be faster, although you’d have to profile it under a given JIT compiler to know for sure. The ‘new’ form is definitely not faster.

    If you look at the disassembled code (compiled by Sun’s JDK 1.5), you’ll see that the ‘new’ form is equivalent to the following code:

    1: double[] tmp = doubleArray; 2: for (int i = 0, y = tmp.length; i < y; i++) { 3:   double var = tmp[i]; 4:   someComplexCalculation(var); 5: } 

    So, you can see that more local variables are used. The assignment of doubleArray to tmp at line 1 is ‘extra’, but it doesn’t occur in the loop, and probably can’t be measured. The assignment to var at line 3 is also extra. If there is a difference in performance, this would be responsible.

    Line 1 might seem unnecessary, but it’s boilerplate to cache the result if the array is computed by a method before entering the loop.

    That said, I would use the new form, unless you need to do something with the index variable. Any performance difference is likely to be optimized away by the JIT compiler at runtime, and the new form is more clear. If you continue to do it ‘by hand’, you may miss out on future optimizations. Generally, a good compiler can optimize ‘stupid’ code well, but stumbles on ‘smart’ code.

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

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

In the Apple documentation for NSRunLoop there is sample code demonstrating suspending execution while
So I am being taught assembly and we have an assignment which is to
I am wondering if there are any differences to derived classes when using abstract
For a complex polygon (ie: self intersecting) the choice between the Winding or the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.