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

  • Home
  • SEARCH
  • 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 8518179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:54:37+00:00 2026-06-11T05:54:37+00:00

As an addendum to this question Java loop efficiency ("for" vs. "foreach") I have

  • 0

As an addendum to this question Java loop efficiency ("for" vs. "foreach")

I have a simple question. Does the enhanced for-loop have a larger memory footprint, or will they both compile to the same construction, making their memory footprint identical and thus making for(Object o : collection) { ... } always better for read-only operations?

My reason for asking is that in a simulator I’m working on, every frame (60/sec) I am running operations against possibly thousands of items in arrays such as faces to draw, to raytrace against, etc. The loop

for(Object o : collection) {
   o.doSomething(); 
}

Looks like it may make a memory-copy of the collection (and I seem to recall that it does from my previous reading) which is okay in most circumstances, but not if you do 30000 raytraces times 60 a second.

On the other hand, it is clear that the loop

count = collection.size();
for(i = 0; i < count; i++) {
   collection[i].doSomething();
}

does everything by reference and has a relatively small footprint even though it is harder to read (though honestly, not much)

Any ideas, folks?

(Note: the impact of the difficulty to read for loops is only evident when you’re several layers in – for single layer fors the gain is extremely minor. I say this from experience… collection[i].property.subcollection[j].row[k].col[l].prop.subtable[m] gets to be a mindbreaker, especially if some of those need to be cast: ((Type3)((Type2)((Type1)collection[i]).property.subcollection[j].row[k]).col[l].prop).subtable[m] for example.)

  • 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-11T05:54:38+00:00Added an answer on June 11, 2026 at 5:54 am

    If you have an ArrayList using hte following pattern can be a micro-optimisation

    for(int i=0, len=list.size(); i < len; i++)
    

    The memory you save is about 16-24 bytes as the for-each loop will always create an Iterator.

    (60/sec) I am running operations against possibly thousands of items in arrays such as faces to draw, to raytrace against,

    For 60,000 per second you are unlikely to notice the difference esp as you are doing relatively significant work with each item.

    Looks like it may make a memory-copy of the collection

    It doesn’t which is why you can get a ConcurrentModicationException if you change the list while looping over it. One workaround for this is to take a copy.

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

Sidebar

Related Questions

As an addendum to this question , what is going on here: #include <string>
(I have also asked this question on SharePoint Overflow .) I have a SharePoint
(This is technically an addendum to an earlier StackOverflow question I had posted, but
This question would be addendum on the last answer in T-SQL stored procedure that
I started with this question: Multiple Javascript gadgets per page . After reading up
Ok so this may be a dumb question (probably some syntax) but Google didn't
I have a structure something like this struct MyStructure :IFoo { } and a
This question is similar to this question about subtracting dates with Python , but
I have this really weird problem in my code (.Net Framework 3.5). So, in
I realize that this question is impossible to answer absolutely, but I'm only after

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.