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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:52:04+00:00 2026-05-13T15:52:04+00:00

Disclaimer: I have looked through this question and this question but they both got

  • 0

Disclaimer: I have looked through this
question
and this question
but they both got derailed by small
details and general
optimization-is-unnecessary concerns.
I really need all the performance I
can get in my current app, which is
receiving-processing-spewing MIDI data
in realtime. Also it needs to scale up
as well as possible.

I am comparing array performance on a high number of reads for small lists to ArrayList and also to just having the variables in hand. I’m finding that an array beats ArrayList by a factor of 2.5 and even beats just having the object references.

What I would like to know is:

  1. Is my benchmark okay? I have switched the order of the tests and number of runs with no change. I’ve also used milliseconds instead of nanoseconds to no avail.
  2. Should I be specifying any Java options to minimize this difference?
  3. If this difference is real, in this case shouldn’t I prefer Test[] to ArrayList<Test> in this situation and put in the code necessary to convert them? Obviously I’m reading a lot more than writing.

JVM is Java 1.6.0_17 on OSX and it is definitely running in Hotspot mode.

  public class ArraysVsLists {

    static int RUNS = 100000;

    public static void main(String[] args) {
        long t1;
        long t2;

        Test test1 = new Test();
        test1.thing = (int)Math.round(100*Math.random());
        Test test2 = new Test();
        test2.thing = (int)Math.round(100*Math.random());

        t1 = System.nanoTime();

        for (int i=0; i<RUNS; i++) {
            test1.changeThing(i);
            test2.changeThing(i);
        }

        t2 = System.nanoTime();
        System.out.println((t2-t1) + " How long NO collection");

        ArrayList<Test> list = new ArrayList<Test>(1);
        list.add(test1);
        list.add(test2);
        // tried this too: helps a tiny tiny bit 
        list.trimToSize();

        t1= System.nanoTime();

        for (int i=0; i<RUNS; i++) {
            for (Test eachTest : list) {
                eachTest.changeThing(i);
            }
        }

        t2 = System.nanoTime();
        System.out.println((t2-t1) + " How long collection");


        Test[] array = new Test[2];
        list.toArray(array);

        t1= System.nanoTime();

        for (int i=0; i<RUNS; i++) {
            for (Test test : array) {
                test.changeThing(i);
            }
        }

        t2 = System.nanoTime();
        System.out.println((t2-t1) + " How long array ");

    }
}

class Test {
    int thing;
    int thing2;
    public void changeThing(int addThis) {
        thing2 = addThis + thing;
    }
}
  • 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-13T15:52:05+00:00Added an answer on May 13, 2026 at 3:52 pm

    Microbenchmarks are very, very hard to get right on a platform like Java. You definitely have to extract the code to be benchmarked into separate methods, run them a few thousand times as warmup and then measure. I’ve done that (code below) and the result is that direct access through references is then three times as fast as through an array, but the collection is still slower by a factor of 2.

    These numbers are based on the JVM options -server -XX:+DoEscapeAnalysis. Without -server, using the collection is drastically slower (but strangely, direct and array access are quite a bit faster, indicating that there is something weird going on). -XX:+DoEscapeAnalysis yields another 30% speedup for the collection, but it’s very much questionabled whether it will work as well for your actual production code.

    Overall my conclusion would be: forget about microbenchmarks, they can too easily be misleading. Measure as close to production code as you can without having to rewrite your entire application.

    import java.util.ArrayList;
    
    public class ArrayTest {
    
        static int RUNS_INNER = 1000;
        static int RUNS_WARMUP = 10000;
        static int RUNS_OUTER = 100000;
    
        public static void main(String[] args) {
            long t1;
            long t2;
    
            Test test1 = new Test();
            test1.thing = (int)Math.round(100*Math.random());
            Test test2 = new Test();
            test2.thing = (int)Math.round(100*Math.random());
    
            for(int i=0; i<RUNS_WARMUP; i++)
            {
                testRefs(test1, test2);            
            }
            t1 = System.nanoTime();
            for(int i=0; i<RUNS_OUTER; i++)
            {
                testRefs(test1, test2);            
            }
    
            t2 = System.nanoTime();
            System.out.println((t2-t1)/1000000.0 + " How long NO collection");
    
            ArrayList<Test> list = new ArrayList<Test>(1);
            list.add(test1);
            list.add(test2);
            // tried this too: helps a tiny tiny bit 
            list.trimToSize();
    
            for(int i=0; i<RUNS_WARMUP; i++)
            {
                testColl(list);
            }
            t1= System.nanoTime();
    
            for(int i=0; i<RUNS_OUTER; i++)
            {
                testColl(list);
            }
    
            t2 = System.nanoTime();
            System.out.println((t2-t1)/1000000.0 + " How long collection");
    
    
            Test[] array = new Test[2];
            list.toArray(array);
    
            for(int i=0; i<RUNS_WARMUP; i++)
            {
                testArr(array);            
            }
            t1= System.nanoTime();
    
            for(int i=0; i<RUNS_OUTER; i++)
            {
                testArr(array);
            }
    
            t2 = System.nanoTime();
            System.out.println((t2-t1)/1000000.0 + " How long array ");
    
        }
    
        private static void testArr(Test[] array)
        {
            for (int i=0; i<RUNS_INNER; i++) {
                for (Test test : array) {
                    test.changeThing(i);
                }
            }
        }
    
        private static void testColl(ArrayList<Test> list)
        {
            for (int i=0; i<RUNS_INNER; i++) {
                for (Test eachTest : list) {
                    eachTest.changeThing(i);
                }
            }
        }
    
        private static void testRefs(Test test1, Test test2)
        {
            for (int i=0; i<RUNS_INNER; i++) {
                test1.changeThing(i);
                test2.changeThing(i);
            }
        }
    }
    
    class Test {
        int thing;
        int thing2;
        public void changeThing(int addThis) {
            thing2 = addThis + thing;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Disclaimer: I realize this is a massive wall of text, but I have done
The Disclaimer First of all, I know this question (or close variations) have been
( Disclaimer: This question is not specific to ASP.NET) I have a control which
Disclaimer: this question may not have practical value, it's more of a puzzle/curiosity question.
Disclaimer: May be a insane question but I have suffered a lot so came
Disclaimer: I have attempted to contact the developer about this plugin, but silence reigns.
Disclaimer: I have not used RoR, and I have not generated tests. But, I
I have searched but have not found my answer. Disclaimer: I am brand new
Disclaimer: I've looked through all the questions I can find and none of them
Disclaimer: I have gone through the related questions, and could not find solutions to

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.