added benchmark tests for set and list performance

This commit is contained in:
mherbaghinyan
2018-08-03 10:32:50 +04:00
parent 7070f25400
commit 2fae078370
3 changed files with 93 additions and 9 deletions
@@ -0,0 +1,53 @@
package com.baeldung.performance;
import org.openjdk.jmh.annotations.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class CollectionsBenchmark {
@State(Scope.Thread)
public static class MyState {
private Set<Employee> employeeSet = new HashSet<>();
private List<Employee> employeeList = new ArrayList<>();
private final long m_count = 16;
private Employee employee = new Employee(100L, "Harry");
@Setup(Level.Invocation)
public void setUp() {
for (long ii = 0; ii < m_count; ii++) {
employeeSet.add(new Employee(ii, "John"));
employeeList.add(new Employee(ii, "John"));
employeeList.add(employee);
employeeSet.add(employee);
}
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MINUTES)
public boolean testArrayList(MyState state) {
return state.employeeList.contains(state.employee);
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MINUTES)
public boolean testHashSet(MyState state) {
return state.employeeSet.contains(state.employee);
}
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
}
@@ -0,0 +1,31 @@
package com.baeldung.performance;
public class Employee {
private Long id;
private String name;
public Employee(Long id, String name) {
this.name = name;
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
if (!id.equals(employee.id)) return false;
return name.equals(employee.name);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + name.hashCode();
return result;
}
}