1: /**
2: *
3: */
4:
5:
6: import gnu.trove.map.hash.TObjectIntHashMap;
7:
8: import java.util.HashMap;
9: import java.util.Map;
10: import java.util.concurrent.ConcurrentHashMap;
11: import java.util.concurrent.ConcurrentMap;
12: import java.util.concurrent.atomic.AtomicLong;
13:
14: import com.google.common.collect.HashMultiset;
15: import com.google.common.collect.Multiset;
16: import com.google.common.util.concurrent.AtomicLongMap;
17:
18: /**
19: * @author Administrator
20: *
21: */
22: public class IntMapTest {
23:
24: /**
25: * @param args
26: */
27: public static void main(String[] args) {
28: // TODO Auto-generated method stub
29: int cycles[] = { 100, 1000, 10000, 100000 };
30: Tester baseLine = new BaseLine();
31: Tester testForNull = new UseNullTest();
32: Tester useAtomicLong = new UseAtomicLong();
33: Tester useTrove = new UseTrove();
34: Tester useMutableInt = new UseMutableInt();
35: Tester useGuava = new UseGuava();
36: Tester useGuava2 = new UseGuava2();
37:
38: for (int i = 0; i < cycles.length; i++) {
39: System.out.println("-----With " + cycles[i] + " cycles-----");
40: baseLine.test(cycles[i]);
41: testForNull.test(cycles[i]);
42: useAtomicLong.test(cycles[i]);
43: useTrove.test(cycles[i]);
44: useMutableInt.test(cycles[i]);
45: useGuava.test(cycles[i]);
46: useGuava2.test(cycles[i]);
47: System.out.println("------------------------");
48: }
49:
50: }
51:
52: }
53:
54: abstract class Tester {
55: long ms;
56: static String[] strs = "abcdefghijklmnopqrstuvwxyz".split("");
57:
58: void pre() {
59: System.out.println("====" + this.getName() + "Test Case ");
60: ms = System.currentTimeMillis();
61: System.out.println("start at " + ms);
62: }
63:
64: void post() {
65: ms = System.currentTimeMillis() - ms;
66: System.out.println("Time used: " + ms + " ms");
67: }
68:
69: abstract void doAction(int cycles);
70:
71: public void test(int cycles) {
72: pre();
73: doAction(cycles);
74: post();
75: }
76:
77: abstract String getName();
78: }
79:
80: class BaseLine extends Tester {
81: final Map<String, Integer> freq = new HashMap<String, Integer>();
82:
83: @Override
84: void doAction(int cycles) {
85: for (int i = 0; i < cycles; i++) {
86: for (String word : strs) {
87: int count = freq.containsKey(word) ? freq.get(word) : 0;
88: freq.put(word, count + 1);
89: }
90: }
91: }
92:
93: @Override
94: String getName() {
95: return "BaseLine";
96: }
97:
98: }
99:
100: class UseNullTest extends Tester {
101: final Map<String, Integer> freq = new HashMap<String, Integer>();
102:
103: @Override
104: void doAction(int cycles) {
105: for (int i = 0; i < cycles; i++) {
106: for (String word : strs) {
107: Integer count = freq.get(word);
108: if (count == null) {
109: freq.put(word, 1);
110: } else {
111: freq.put(word, count + 1);
112: }
113: }
114: }
115: }
116:
117: @Override
118: String getName() {
119: return "TestForNull";
120: }
121:
122: }
123:
124: class UseAtomicLong extends Tester {
125: final ConcurrentMap<String, AtomicLong> map = new ConcurrentHashMap<String, AtomicLong>();
126:
127: @Override
128: void doAction(int cycles) {
129: for (int i = 0; i < cycles; i++) {
130: for (String word : strs) {
131: map.putIfAbsent(word, new AtomicLong(0));
132: map.get(word).incrementAndGet();
133: }
134: }
135: }
136:
137: @Override
138: String getName() {
139: return "AtomicLong";
140: }
141:
142: }
143:
144: class UseTrove extends Tester {
145: final TObjectIntHashMap<String> freq = new TObjectIntHashMap<String>();
146:
147: @Override
148: void doAction(int cycles) {
149: for (int i = 0; i < cycles; i++) {
150: for (String word : strs) {
151: freq.adjustOrPutValue(word, 1, 1);
152: }
153: }
154: }
155:
156: @Override
157: String getName() {
158: return "Trove";
159: }
160:
161: }
162:
163: class MutableInt {
164: int value = 1; // note that we start at 1 since we're counting
165:
166: public void increment() {
167: ++value;
168: }
169:
170: public int get() {
171: return value;
172: }
173: }
174:
175: class UseMutableInt extends Tester {
176: Map<String, MutableInt> freq = new HashMap<String, MutableInt>();
177:
178: @Override
179: void doAction(int cycles) {
180: for (int i = 0; i < cycles; i++) {
181: for (String word : strs) {
182: MutableInt count = freq.get(word);
183: if (count == null) {
184: freq.put(word, new MutableInt());
185: } else {
186: count.increment();
187: }
188: }
189: }
190: }
191:
192: @Override
193: String getName() {
194: return "MutableInt";
195: }
196:
197: }
198:
199: class UseGuava extends Tester {
200: AtomicLongMap<String> map = AtomicLongMap.create();
201:
202: @Override
203: void doAction(int cycles) {
204: for (int i = 0; i < cycles; i++) {
205: for (String word : strs) {
206: map.getAndIncrement(word);
207: }
208: }
209: }
210:
211: @Override
212: String getName() {
213: return "Guava AtomicLongMap";
214: }
215:
216: }
217:
218: class UseGuava2 extends Tester {
219: Multiset<String> bag = HashMultiset.create();
220:
221: @Override
222: void doAction(int cycles) {
223: for (int i = 0; i < cycles; i++) {
224: for (String word : strs) {
225: bag.add(word);
226: }
227: }
228: }
229:
230: @Override
231: String getName() {
232: return "Guava HashMultiSet";
233: }
234:
235: }