Array or List in Java. Which is faster?

后端 未结 30 2137
滥情空心
滥情空心 2020-11-22 04:30

I have to keep thousands of strings in memory to be accessed serially in Java. Should I store them in an array or should I use some kind of List ?

Since arrays keep

30条回答
  •  执念已碎
    2020-11-22 05:03

    None of the answers had information that I was interested in - repetitive scan of the same array many many times. Had to create a JMH test for this.

    Results (Java 1.8.0_66 x32, iterating plain array is at least 5 times quicker than ArrayList):

    Benchmark                    Mode  Cnt   Score   Error  Units
    MyBenchmark.testArrayForGet  avgt   10   8.121 ? 0.233  ms/op
    MyBenchmark.testListForGet   avgt   10  37.416 ? 0.094  ms/op
    MyBenchmark.testListForEach  avgt   10  75.674 ? 1.897  ms/op
    

    Test

    package my.jmh.test;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.openjdk.jmh.annotations.Benchmark;
    import org.openjdk.jmh.annotations.BenchmarkMode;
    import org.openjdk.jmh.annotations.Fork;
    import org.openjdk.jmh.annotations.Measurement;
    import org.openjdk.jmh.annotations.Mode;
    import org.openjdk.jmh.annotations.OutputTimeUnit;
    import org.openjdk.jmh.annotations.Scope;
    import org.openjdk.jmh.annotations.State;
    import org.openjdk.jmh.annotations.Warmup;
    
    @State(Scope.Benchmark)
    @Fork(1)
    @Warmup(iterations = 5, timeUnit = TimeUnit.SECONDS)
    @Measurement(iterations = 10)
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public class MyBenchmark {
    
        public final static int ARR_SIZE = 100;
        public final static int ITER_COUNT = 100000;
    
        String arr[] = new String[ARR_SIZE];
        List list = new ArrayList<>(ARR_SIZE);
    
        public MyBenchmark() {
            for( int i = 0; i < ARR_SIZE; i++ ) {
                list.add(null);
            }
        }
    
        @Benchmark
        public void testListForEach() {
            int count = 0;
            for( int i = 0; i < ITER_COUNT; i++ ) {
                for( String str : list ) {
                    if( str != null )
                        count++;
                }
            }
            if( count > 0 )
                System.out.print(count);
        }
    
        @Benchmark
        public void testListForGet() {
            int count = 0;
            for( int i = 0; i < ITER_COUNT; i++ ) {
                for( int j = 0; j < ARR_SIZE; j++ ) {
                    if( list.get(j) != null )
                        count++;
                }
            }
            if( count > 0 )
                System.out.print(count);
        }
    
        @Benchmark
        public void testArrayForGet() {
            int count = 0;
            for( int i = 0; i < ITER_COUNT; i++ ) {
                for( int j = 0; j < ARR_SIZE; j++ ) {
                    if( arr[j] != null )
                        count++;
                }
            }
            if( count > 0 )
                System.out.print(count);
        }
    
    }
    

提交回复
热议问题