99import java .util .concurrent .TimeUnit ;
1010
1111@ BenchmarkMode (Mode .AverageTime )
12- @ OutputTimeUnit (TimeUnit .MICROSECONDS )
12+ @ OutputTimeUnit (TimeUnit .NANOSECONDS )
1313@ Warmup (iterations = 10 )
1414public class ArrayListBenchmark {
1515
1616 @ State (Scope .Thread )
1717 public static class MyState {
1818
1919 List <Employee > employeeList = new ArrayList <>();
20+ Vector <Employee > employeeVector = new Vector <>();
2021 //LinkedList<Employee> employeeList = new LinkedList<>();
2122
2223 long iterations = 100000 ;
@@ -29,9 +30,11 @@ public static class MyState {
2930 public void setUp () {
3031 for (long i = 0 ; i < iterations ; i ++) {
3132 employeeList .add (new Employee (i , "John" ));
33+ employeeVector .add (new Employee (i , "John" ));
3234 }
3335
3436 employeeList .add (employee );
37+ employeeVector .add (employee );
3538 employeeIndex = employeeList .indexOf (employee );
3639 }
3740 }
@@ -46,6 +49,11 @@ public boolean testContains(ArrayListBenchmark.MyState state) {
4649 return state .employeeList .contains (state .employee );
4750 }
4851
52+ @ Benchmark
53+ public boolean testContainsVector (ArrayListBenchmark .MyState state ) {
54+ return state .employeeVector .contains (state .employee );
55+ }
56+
4957 @ Benchmark
5058 public int testIndexOf (ArrayListBenchmark .MyState state ) {
5159 return state .employeeList .indexOf (state .employee );
@@ -56,19 +64,24 @@ public Employee testGet(ArrayListBenchmark.MyState state) {
5664 return state .employeeList .get (state .employeeIndex );
5765 }
5866
67+ @ Benchmark
68+ public Employee testVectorGet (ArrayListBenchmark .MyState state ) {
69+ return state .employeeVector .get (state .employeeIndex );
70+ }
71+
5972 @ Benchmark
6073 public boolean testRemove (ArrayListBenchmark .MyState state ) {
6174 return state .employeeList .remove (state .employee );
6275 }
6376
64- // @Benchmark
65- // public void testAdd(ArrayListBenchmark.MyState state) {
66- // state.employeeList.add(new Employee(state.iterations + 1, "John"));
67- // }
77+ @ Benchmark
78+ public void testAdd (ArrayListBenchmark .MyState state ) {
79+ state .employeeList .add (new Employee (state .iterations + 1 , "John" ));
80+ }
6881
6982 public static void main (String [] args ) throws Exception {
7083 Options options = new OptionsBuilder ()
71- .include (ArrayListBenchmark .class .getSimpleName ()).threads (1 )
84+ .include (ArrayListBenchmark .class .getSimpleName ()).threads (3 )
7285 .forks (1 ).shouldFailOnError (true )
7386 .shouldDoGC (true )
7487 .jvmArgs ("-server" ).build ();
0 commit comments