9
9
import java .util .concurrent .TimeUnit ;
10
10
11
11
@ BenchmarkMode (Mode .AverageTime )
12
- @ OutputTimeUnit (TimeUnit .MICROSECONDS )
12
+ @ OutputTimeUnit (TimeUnit .NANOSECONDS )
13
13
@ Warmup (iterations = 10 )
14
14
public class ArrayListBenchmark {
15
15
16
16
@ State (Scope .Thread )
17
17
public static class MyState {
18
18
19
19
List <Employee > employeeList = new ArrayList <>();
20
+ Vector <Employee > employeeVector = new Vector <>();
20
21
//LinkedList<Employee> employeeList = new LinkedList<>();
21
22
22
23
long iterations = 100000 ;
@@ -29,9 +30,11 @@ public static class MyState {
29
30
public void setUp () {
30
31
for (long i = 0 ; i < iterations ; i ++) {
31
32
employeeList .add (new Employee (i , "John" ));
33
+ employeeVector .add (new Employee (i , "John" ));
32
34
}
33
35
34
36
employeeList .add (employee );
37
+ employeeVector .add (employee );
35
38
employeeIndex = employeeList .indexOf (employee );
36
39
}
37
40
}
@@ -46,6 +49,11 @@ public boolean testContains(ArrayListBenchmark.MyState state) {
46
49
return state .employeeList .contains (state .employee );
47
50
}
48
51
52
+ @ Benchmark
53
+ public boolean testContainsVector (ArrayListBenchmark .MyState state ) {
54
+ return state .employeeVector .contains (state .employee );
55
+ }
56
+
49
57
@ Benchmark
50
58
public int testIndexOf (ArrayListBenchmark .MyState state ) {
51
59
return state .employeeList .indexOf (state .employee );
@@ -56,19 +64,24 @@ public Employee testGet(ArrayListBenchmark.MyState state) {
56
64
return state .employeeList .get (state .employeeIndex );
57
65
}
58
66
67
+ @ Benchmark
68
+ public Employee testVectorGet (ArrayListBenchmark .MyState state ) {
69
+ return state .employeeVector .get (state .employeeIndex );
70
+ }
71
+
59
72
@ Benchmark
60
73
public boolean testRemove (ArrayListBenchmark .MyState state ) {
61
74
return state .employeeList .remove (state .employee );
62
75
}
63
76
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
+ }
68
81
69
82
public static void main (String [] args ) throws Exception {
70
83
Options options = new OptionsBuilder ()
71
- .include (ArrayListBenchmark .class .getSimpleName ()).threads (1 )
84
+ .include (ArrayListBenchmark .class .getSimpleName ()).threads (3 )
72
85
.forks (1 ).shouldFailOnError (true )
73
86
.shouldDoGC (true )
74
87
.jvmArgs ("-server" ).build ();
0 commit comments