commit
1fdf586336
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.java.list;
|
||||||
|
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
public class VectorExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Vector<String> vector = new Vector<>();
|
||||||
|
vector.add("baeldung");
|
||||||
|
vector.add("Vector");
|
||||||
|
vector.add("example");
|
||||||
|
|
||||||
|
Enumeration e = vector.elements();
|
||||||
|
while(e.hasMoreElements()){
|
||||||
|
System.out.println(e.nextElement());
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator<String> iterator = vector.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
System.out.println(iterator.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,7 +9,7 @@ import java.util.*;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
@Warmup(iterations = 10)
|
@Warmup(iterations = 10)
|
||||||
public class ArrayListBenchmark {
|
public class ArrayListBenchmark {
|
||||||
|
|
||||||
@ -17,6 +17,7 @@ public class ArrayListBenchmark {
|
|||||||
public static class MyState {
|
public static class MyState {
|
||||||
|
|
||||||
List<Employee> employeeList = new ArrayList<>();
|
List<Employee> employeeList = new ArrayList<>();
|
||||||
|
Vector<Employee> employeeVector = new Vector<>();
|
||||||
//LinkedList<Employee> employeeList = new LinkedList<>();
|
//LinkedList<Employee> employeeList = new LinkedList<>();
|
||||||
|
|
||||||
long iterations = 100000;
|
long iterations = 100000;
|
||||||
@ -29,9 +30,11 @@ public class ArrayListBenchmark {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
for (long i = 0; i < iterations; i++) {
|
for (long i = 0; i < iterations; i++) {
|
||||||
employeeList.add(new Employee(i, "John"));
|
employeeList.add(new Employee(i, "John"));
|
||||||
|
employeeVector.add(new Employee(i, "John"));
|
||||||
}
|
}
|
||||||
|
|
||||||
employeeList.add(employee);
|
employeeList.add(employee);
|
||||||
|
employeeVector.add(employee);
|
||||||
employeeIndex = employeeList.indexOf(employee);
|
employeeIndex = employeeList.indexOf(employee);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -46,6 +49,11 @@ public class ArrayListBenchmark {
|
|||||||
return state.employeeList.contains(state.employee);
|
return state.employeeList.contains(state.employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public boolean testContainsVector(ArrayListBenchmark.MyState state) {
|
||||||
|
return state.employeeVector.contains(state.employee);
|
||||||
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public int testIndexOf(ArrayListBenchmark.MyState state) {
|
public int testIndexOf(ArrayListBenchmark.MyState state) {
|
||||||
return state.employeeList.indexOf(state.employee);
|
return state.employeeList.indexOf(state.employee);
|
||||||
@ -56,19 +64,24 @@ public class ArrayListBenchmark {
|
|||||||
return state.employeeList.get(state.employeeIndex);
|
return state.employeeList.get(state.employeeIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public Employee testVectorGet(ArrayListBenchmark.MyState state) {
|
||||||
|
return state.employeeVector.get(state.employeeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public boolean testRemove(ArrayListBenchmark.MyState state) {
|
public boolean testRemove(ArrayListBenchmark.MyState state) {
|
||||||
return state.employeeList.remove(state.employee);
|
return state.employeeList.remove(state.employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Benchmark
|
@Benchmark
|
||||||
// public void testAdd(ArrayListBenchmark.MyState state) {
|
public void testAdd(ArrayListBenchmark.MyState state) {
|
||||||
// state.employeeList.add(new Employee(state.iterations + 1, "John"));
|
state.employeeList.add(new Employee(state.iterations + 1, "John"));
|
||||||
// }
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Options options = new OptionsBuilder()
|
Options options = new OptionsBuilder()
|
||||||
.include(ArrayListBenchmark.class.getSimpleName()).threads(1)
|
.include(ArrayListBenchmark.class.getSimpleName()).threads(3)
|
||||||
.forks(1).shouldFailOnError(true)
|
.forks(1).shouldFailOnError(true)
|
||||||
.shouldDoGC(true)
|
.shouldDoGC(true)
|
||||||
.jvmArgs("-server").build();
|
.jvmArgs("-server").build();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user