Java – vector vs ArrayList performance test
Everyone said that vectors should be used because of performance (causing vectors to synchronize after each operation and thing) I wrote a simple test:
import java.util.ArrayList; import java.util.Date; import java.util.Vector; public class ComparePerformance { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); Vector<Integer> vector = new Vector<Integer>(); int size = 10000000; int listSum = 0; int vectorSum = 0; long startList = new Date().getTime(); for (int i = 0; i < size; i++) { list.add(new Integer(1)); } for (Integer integer : list) { listSum += integer; } long endList = new Date().getTime(); System.out.println("List time: " + (endList - startList)); long startVector = new Date().getTime(); for (int i = 0; i < size; i++) { vector.add(new Integer(1)); } for (Integer integer : list) { vectorSum += integer; } long endVector = new Date().getTime(); System.out.println("Vector time: " + (endVector - startVector)); } }
The results are as follows:
List time: 4360 Vector time: 4103
Based on this, it seems that the vector performance in iteration and reading is slightly better Maybe it's a stupid question, or I made the wrong assumption - can anyone explain it?
Solution
You wrote a naive micro benchmark Microblog technology on JVM is a very tricky business. It is not even easy to list all traps, but here are some classic ones:
>You must warm up the code; > You must control garbage collection pauses; > System. Currenttimemillis is imprecise, but you don't seem to know this method (your new date() Gettime () is equivalent, but slower)
If you want to do this correctly, check Oracle's jmh tools or Google's caliper
My test results
Since I'm interested in reading these numbers myself, here is the output of jmh 1、 Test code:
public class Benchmark1 { static Integer[] ints = new Integer[0]; static { final List<Integer> list = new ArrayList(asList(1,2,3,4,5,6,7,8,9,10)); for (int i = 0; i < 5; i++) list.addAll(list); ints = list.toArray(ints); } static List<Integer> intList = Arrays.asList(ints); static Vector<Integer> vec = new Vector<Integer>(intList); static List<Integer> list = new ArrayList<Integer>(intList); @GenerateMicroBenchmark public Vector<Integer> testVectorAdd() { final Vector<Integer> v = new Vector<Integer>(); for (Integer i : ints) v.add(i); return v; } @GenerateMicroBenchmark public long testVectorTraverse() { long sum = (long)Math.random()*10; for (int i = 0; i < vec.size(); i++) sum += vec.get(i); return sum; } @GenerateMicroBenchmark public List<Integer> testArrayListAdd() { final List<Integer> l = new ArrayList<Integer>(); for (Integer i : ints) l.add(i); return l; } @GenerateMicroBenchmark public long testArrayListTraverse() { long sum = (long)Math.random()*10; for (int i = 0; i < list.size(); i++) sum += list.get(i); return sum; } }
The results are as follows:
testArrayListAdd 234.896 ops/msec testVectorAdd 274.886 ops/msec testArrayListTraverse 1718.711 ops/msec testVectorTraverse 34.843 ops/msec
Please note the following:
>Add method at... I'm creating a new local collection The JIT compiler uses this fact and omits the locking of the vector method - so almost the same performance; > In the... Traversal method, I am reading from the global collection; Locks cannot be omitted, which is where the real performance loss of vector is shown
The main content of this should be: the performance model on the JVM is very complex and sometimes even unstable Inferring from the micro benchmark measurement, even if it is completed under completely appropriate conditions, it may lead to dangerous misprediction of the performance of the production system