OnJava8-Examples/containersindepth/MapPerformance.java

79 lines
2.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// containersindepth/MapPerformance.java
2015-06-15 17:47:35 -07:00
// Demonstrates performance differences in Maps.
// {Args: 100 5000} Small to keep build testing short
import java.util.*;
public class MapPerformance {
static List<Test<Map<Integer,Integer>>> tests =
new ArrayList<>();
static {
tests.add(new Test<Map<Integer,Integer>>("put") {
@Override
int test(Map<Integer,Integer> map, TestParam tp) {
int loops = tp.loops;
int size = tp.size;
for(int i = 0; i < loops; i++) {
map.clear();
for(int j = 0; j < size; j++)
map.put(j, j);
}
return loops * size;
}
});
tests.add(new Test<Map<Integer,Integer>>("get") {
@Override
int test(Map<Integer,Integer> map, 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++)
map.get(j);
return loops * span;
}
});
tests.add(new Test<Map<Integer,Integer>>("iterate") {
@Override
int test(Map<Integer,Integer> map, TestParam tp) {
int loops = tp.loops * 10;
for(int i = 0; i < loops; i ++) {
Iterator it = map.entrySet().iterator();
while(it.hasNext())
it.next();
}
return loops * map.size();
}
});
}
public static void main(String[] args) {
if(args.length > 0)
Tester.defaultParams = TestParam.array(args);
Tester.run(new TreeMap<>(), tests);
Tester.run(new HashMap<>(), tests);
Tester.run(new LinkedHashMap<>(),tests);
Tester.run(
new IdentityHashMap<>(), tests);
Tester.run(new WeakHashMap<>(), tests);
Tester.run(new Hashtable<>(), tests);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
---------- TreeMap ----------
2015-09-07 11:44:36 -06:00
size put get iterate
2015-06-15 17:47:35 -07:00
100 137 99 24
---------- HashMap ----------
2015-09-07 11:44:36 -06:00
size put get iterate
2015-06-15 17:47:35 -07:00
100 47 19 29
------- LinkedHashMap -------
2015-09-07 11:44:36 -06:00
size put get iterate
2015-06-15 17:47:35 -07:00
100 59 13 11
------ IdentityHashMap ------
2015-09-07 11:44:36 -06:00
size put get iterate
2015-06-15 17:47:35 -07:00
100 74 71 23
-------- WeakHashMap --------
2015-09-07 11:44:36 -06:00
size put get iterate
2015-06-15 17:47:35 -07:00
100 43 16 15
--------- Hashtable ---------
2015-09-07 11:44:36 -06:00
size put get iterate
2015-06-15 17:47:35 -07:00
100 32 27 17
2015-09-07 11:44:36 -06:00
*/