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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:27:28+00:00 2026-05-24T02:27:28+00:00

I just came across this seemingly innocuous comment , benchmarking ArrayList vs a raw

  • 0

I just came across this seemingly innocuous comment, benchmarking ArrayList vs a raw String array. It’s from a couple years ago, but the OP writes

I did notice that using for String s: stringsList was about 50% slower than using an old-style for-loop to access the list. Go figure…

Nobody commented on it in the original post, and the test seemed a little dubious (too short to be accurate), but I nearly fell out of my chair when I read it. I’ve never benchmarked an enhanced loop against a “traditional” one, but I’m currently working on a project that does hundreds of millions of iterations over ArrayList instances using enhanced loops so this is a concern to me.

I’m going to do some benchmarking and post my findings here, but this is obviously a big concern to me. I could find precious little info online about relative performance, except for a couple offhand mentions that enhanced loops for ArrayLists do run a lot slower under Android.

Has anybody experienced this? Does such a performance gap still exist? I’ll post my findings here, but was very surprised to read it. I suspect that if this performance gap did exist, it has been fixed in more modern VM’s, but I guess now I’ll have to do some testing and confirm.

Update: I made some changes to my code, but was already suspecting what others here have already pointed out: sure the enhanced for loop is slower, but outside of very trivial tight loops, the cost should be a miniscule fraction of the cost of the logic of the loop. In my case, even though I’m iterating over very large lists of strings using enhanced loops, my logic inside the loop is complex enough that I couldn’t even measure a difference after switching to index-based loops.

TL;DR: enhanced loops are indeed slower than a traditional index-based loop over an arraylist; but for most applications the difference should be negligible.

  • 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-24T02:27:28+00:00Added an answer on May 24, 2026 at 2:27 am

    The problem you have is that using an Iterator will be slower than using a direct lookup. On my machine the difference is about 0.13 ns per iteration. Using an array instead saves about 0.15 ns per iteration. This should be trivial in 99% of situations.

    public static void main(String... args) {
        int testLength = 100 * 1000 * 1000;
        String[] stringArray = new String[testLength];
        Arrays.fill(stringArray, "a");
        List<String> stringList = new ArrayList<String>(Arrays.asList(stringArray));
        {
            long start = System.nanoTime();
            long total = 0;
            for (String str : stringArray) {
                total += str.length();
            }
            System.out.printf("The for each Array loop time was %.2f ns total=%d%n", (double) (System.nanoTime() - start) / testLength, total);
        }
        {
            long start = System.nanoTime();
            long total = 0;
            for (int i = 0, stringListSize = stringList.size(); i < stringListSize; i++) {
                String str = stringList.get(i);
                total += str.length();
            }
            System.out.printf("The for/get List loop time was %.2f ns total=%d%n", (double) (System.nanoTime() - start) / testLength, total);
        }
        {
            long start = System.nanoTime();
            long total = 0;
            for (String str : stringList) {
                total += str.length();
            }
            System.out.printf("The for each List loop time was %.2f ns total=%d%n", (double) (System.nanoTime() - start) / testLength, total);
        }
    }
    

    When run with one billion entries entries prints (using Java 6 update 26.)

    The for each Array loop time was 0.76 ns total=1000000000
    The for/get List loop time was 0.91 ns total=1000000000
    The for each List loop time was 1.04 ns total=1000000000
    

    When run with one billion entries entries prints (using OpenJDK 7.)

    The for each Array loop time was 0.76 ns total=1000000000
    The for/get List loop time was 0.91 ns total=1000000000
    The for each List loop time was 1.04 ns total=1000000000
    

    i.e. exactly the same. 😉

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

Sidebar

Related Questions

Possible Duplicate: curly braces in string Just came across this piece of code and
Just came across this quote in a book on OOP that I'm reading, A
Just came across this website . Feature 9 is memory management and they claim
I just came across this code and a few Google searches turn up no
I just came across this question about initializing local variables. Many of the answers
I just came across this idiom in some open-source Python, and I choked on
I just came across a C++ SDK that makes heavy use of this really
I was just reading through Learning Python by Mark Lutz and came across this
Just came across this guy that left me stunned: gcc -E -dM - </dev/null
Just came across this article about NOSQL patterns (not mine). It's covers lots of

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.