OnJava8-Examples/containers/SetPerformance.java

76 lines
2.2 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: containers/SetPerformance.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// Demonstrates performance differences in Sets.
// {Args: 100 5000} Small to keep build testing short
import java.util.*;
public class SetPerformance {
static List<Test<Set<Integer>>> tests =
2015-05-05 11:20:13 -07:00
new ArrayList<>();
2015-04-20 15:36:01 -07:00
static {
tests.add(new Test<Set<Integer>>("add") {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
int test(Set<Integer> set, TestParam tp) {
int loops = tp.loops;
int size = tp.size;
for(int i = 0; i < loops; i++) {
set.clear();
for(int j = 0; j < size; j++)
set.add(j);
}
return loops * size;
}
});
tests.add(new Test<Set<Integer>>("contains") {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
int test(Set<Integer> set, TestParam tp) {
int loops = tp.loops;
int span = tp.size * 2;
for(int i = 0; i < loops; i++)
for(int j = 0; j < span; j++)
set.contains(j);
return loops * span;
}
});
tests.add(new Test<Set<Integer>>("iterate") {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
int test(Set<Integer> set, TestParam tp) {
int loops = tp.loops * 10;
for(int i = 0; i < loops; i++) {
Iterator<Integer> it = set.iterator();
while(it.hasNext())
it.next();
}
return loops * set.size();
}
});
}
public static void main(String[] args) {
if(args.length > 0)
Tester.defaultParams = TestParam.array(args);
Tester.fieldWidth = 10;
2015-05-05 11:20:13 -07:00
Tester.run(new TreeSet<>(), tests);
Tester.run(new HashSet<>(), tests);
Tester.run(new LinkedHashSet<>(), tests);
2015-04-20 15:36:01 -07:00
}
} /* Output: (Sample)
------------- TreeSet -------------
size add contains iterate
10 746 173 89
100 501 264 68
1000 714 410 69
10000 1975 552 69
------------- HashSet -------------
size add contains iterate
10 308 91 94
100 178 75 73
1000 216 110 72
10000 711 215 100
---------- LinkedHashSet ----------
size add contains iterate
10 350 65 83
100 270 74 55
1000 303 111 54
10000 1615 256 58
*///:~