Skip to content

Commit 1fdf586

Browse files
authored
Merge pull request eugenp#5942 from MherBaghinyan/BAEL-2440
Bael 2440
2 parents 43b0770 + 725076b commit 1fdf586

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.java.list;
2+
3+
import java.util.Enumeration;
4+
import java.util.Iterator;
5+
import java.util.Vector;
6+
7+
public class VectorExample {
8+
9+
public static void main(String[] args) {
10+
11+
Vector<String> vector = new Vector<>();
12+
vector.add("baeldung");
13+
vector.add("Vector");
14+
vector.add("example");
15+
16+
Enumeration e = vector.elements();
17+
while(e.hasMoreElements()){
18+
System.out.println(e.nextElement());
19+
}
20+
21+
Iterator<String> iterator = vector.iterator();
22+
while (iterator.hasNext()) {
23+
System.out.println(iterator.next());
24+
}
25+
}
26+
27+
}

core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import java.util.concurrent.TimeUnit;
1010

1111
@BenchmarkMode(Mode.AverageTime)
12-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
12+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
1313
@Warmup(iterations = 10)
1414
public 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

Comments
 (0)