KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > benchmark > Main


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19 package gnu.trove.benchmark;
20
21 import gnu.trove.*;
22 import java.util.*;
23
24 /**
25  *
26  * Created: Sat Nov 3 18:17:56 2001
27  *
28  * @author Eric D. Friedman
29  * @version $Id: Main.java,v 1.4 2006/11/15 21:14:47 robeden Exp $
30  */

31
32 public class Main {
33     static final int ITERATIONS = 25;
34
35     static final int SET_SIZE = 100000;
36     static final List dataset = new ArrayList(SET_SIZE);
37     static {
38         for (int i = 0; i < SET_SIZE; i++) {
39             dataset.add(new Integer JavaDoc(i));
40         }
41     }
42     
43     public static Operation getSetOperation() {
44         
45         return new Operation() {
46                 public void theirs() {
47                     Set s = new HashSet(SET_SIZE);
48                     for (Iterator i = dataset.iterator(); i.hasNext();) {
49                         s.add(i.next());
50                     }
51                 }
52
53                 public void ours() {
54                     Set s = new THashSet(SET_SIZE);
55                     for (Iterator i = dataset.iterator(); i.hasNext();) {
56                         s.add(i.next());
57                     }
58                 }
59
60                 public String JavaDoc toString() {
61                     return "compares " + dataset.size() + " Set.add() operations";
62                 }
63
64                 public int getIterationCount() {
65                     return ITERATIONS;
66                 }
67             };
68     }
69
70     public static Operation getLinkedListAddOp() {
71         try {
72             Class.forName( "gnu.trove.TLinkableAdapter" );
73         }
74         catch( Throwable JavaDoc t ) {
75             return new Operation() {
76
77                 public int getIterationCount() {
78                     return 1;
79                 }
80
81                 public void ours() {}
82                 public void theirs() {}
83
84                 public String JavaDoc toString() {
85                     return "(**UNAVAILABLE**) compares " + dataset.size() +
86                         " LinkedList.add() operations";
87                 }
88             };
89         }
90
91         final List data = new ArrayList(100000);
92         for (int i = 0; i < 100000; i++) {
93             data.add(new TLinkableAdapter());
94         }
95         
96
97         return new Operation() {
98
99                 public void theirs() {
100                     List l = new LinkedList();
101                     for (Iterator i = data.iterator(); i.hasNext();) {
102                         l.add(i.next());
103                     }
104                 }
105
106                 public void ours() {
107                     List l = new TLinkedList();
108                     for (Iterator i = data.iterator(); i.hasNext();) {
109                         l.add(i.next());
110                     }
111                 }
112
113                 public String JavaDoc toString() {
114                     return "compares " + dataset.size() + " LinkedList.add() operations";
115                 }
116
117                 public int getIterationCount() {
118                     return ITERATIONS;
119                 }
120             };
121     }
122
123     static Operation getContainsOp() {
124         final Set theirs = new HashSet(dataset.size());
125         theirs.addAll(dataset);
126         final Set ours = new THashSet(dataset.size());
127         ours.addAll(dataset);
128
129         return new Operation() {
130                 public void theirs() {
131                     for (int i = 0; i < dataset.size(); i += 5) {
132                         theirs.contains(dataset.get(i));
133                     }
134                 }
135
136                 public void ours() {
137                     for (int i = 0; i < dataset.size(); i += 5) {
138                         ours.contains(dataset.get(i));
139                     }
140                 }
141
142                 public String JavaDoc toString() {
143                     return "compares " + dataset.size() / 5 + " Set.contains() operations";
144                 }
145
146                 public int getIterationCount() {
147                     return ITERATIONS;
148                 }
149             };
150     }
151
152
153     static Operation getRandomSetContainsOp() {
154         final Set theirs = new HashSet(SET_SIZE);
155         final Set ours = new THashSet(SET_SIZE);
156         Random rand = new Random(9999L);
157
158         for (int i = 0; i < SET_SIZE; i++) {
159             Integer JavaDoc x = new Integer JavaDoc(rand.nextInt());
160             theirs.add(x);
161             ours.add(x);
162         }
163
164         Random rand2 = new Random(9998L);
165         final List query = new ArrayList(SET_SIZE);
166         int match = 0;
167         for (int i = 0; i < SET_SIZE; i++) {
168             Integer JavaDoc x = new Integer JavaDoc(rand.nextInt());
169             query.add(x);
170             if (theirs.contains(x)) {
171                 match++;
172             }
173         }
174
175         final int success = match;
176
177         return new Operation() {
178                 public void theirs() {
179                     for (Iterator i = query.iterator(); i.hasNext();) {
180                         theirs.contains(i.next());
181                     }
182                 }
183
184                 public void ours() {
185                     for (Iterator i = query.iterator(); i.hasNext();) {
186                         ours.contains(i.next());
187                     }
188                 }
189
190                 public String JavaDoc toString() {
191                     return "compares " + SET_SIZE + " Set.contains() operations. "
192                         + success + " are actually present in set";
193                 }
194
195                 public int getIterationCount() {
196                     return ITERATIONS;
197                 }
198             };
199     }
200
201     static Operation getMapPutOp() {
202         return new Operation() {
203                 public void theirs() {
204                     Map theirs = new HashMap(dataset.size());
205                     for (Iterator i = dataset.iterator();i.hasNext();) {
206                         Object JavaDoc o = i.next();
207                         theirs.put(o,o);
208                     }
209                 }
210
211                 public void ours() {
212                     Map ours = new THashMap(dataset.size());
213                     for (Iterator i = dataset.iterator();i.hasNext();) {
214                         Object JavaDoc o = i.next();
215                         ours.put(o,o);
216                     }
217                 }
218
219                 public String JavaDoc toString() {
220                     return "compares " + dataset.size() + " Map.put() operations";
221                 }
222
223                 public int getIterationCount() {
224                     return ITERATIONS;
225                 }
226             };
227     }
228
229     static Operation getIterationOp() {
230         final Map theirMap = new HashMap(dataset.size());
231         final Map ourMap = new THashMap(dataset.size());
232         for (Iterator i = dataset.iterator();i.hasNext();) {
233             Object JavaDoc o = i.next();
234             theirMap.put(o,o);
235             ourMap.put(o,o);
236         }
237         
238         return new Operation() {
239                 public void theirs() {
240                     Map m = theirMap;
241                     Iterator i = m.keySet().iterator();
242                     for (int size = m.size(); size-- > 0;) {
243                         Object JavaDoc o = i.next();
244                     }
245                 }
246
247                 public void ours() {
248                     Map m = ourMap;
249                     Iterator i = m.keySet().iterator();
250                     for (int size = m.size(); size-- > 0;) {
251                         Object JavaDoc o = i.next();
252                     }
253                 }
254
255                 public String JavaDoc toString() {
256                     return "compares Iterator.next() over " + dataset.size() + " map keys";
257                 }
258
259                 public int getIterationCount() {
260                     return ITERATIONS;
261                 }
262             };
263     }
264
265     static Operation getIterationWithHasNextOp() {
266         final Map theirMap = new HashMap(dataset.size());
267         final Map ourMap = new THashMap(dataset.size());
268         for (Iterator i = dataset.iterator();i.hasNext();) {
269             Object JavaDoc o = i.next();
270             theirMap.put(o,o);
271             ourMap.put(o,o);
272         }
273         
274         return new Operation() {
275                 public void theirs() {
276                     Map m = theirMap;
277                     Iterator i = m.keySet().iterator();
278                     while (i.hasNext()) {
279                         Object JavaDoc o = i.next();
280                     }
281                 }
282
283                 public void ours() {
284                     Map m = ourMap;
285                     Iterator i = m.keySet().iterator();
286                     while (i.hasNext()) {
287                         Object JavaDoc o = i.next();
288                     }
289                 }
290
291                 public String JavaDoc toString() {
292                     return "compares Iterator.hasNext()/ Iterator.next() over " + theirMap.size() + " keys";
293                 }
294
295                 public int getIterationCount() {
296                     return ITERATIONS;
297                 }
298             };
299     }
300
301     static Operation getIntMapPut() {
302         return new Operation() {
303                 public void theirs() {
304                 }
305
306                 public void ours() {
307                     TIntIntHashMap ours = new TIntIntHashMap(SET_SIZE);
308                     for (int i = dataset.size(); i-- > 0;) {
309                         ours.put(i,i);
310                     }
311                 }
312
313                 public String JavaDoc toString() {
314                     return dataset.size() + " entry primitive int map.put timing run; no basis for comparison";
315                 }
316
317                 public int getIterationCount() {
318                     return ITERATIONS;
319                 }
320             };
321     }
322
323     static Operation getSumSetOperation() {
324         final Set theirSet = new HashSet(dataset.size());
325         final THashSet ourSet = new THashSet(dataset.size());
326         theirSet.addAll(dataset);
327         ourSet.addAll(dataset);
328         final TObjectProcedure proc = new TObjectProcedure() {
329                 int sum = 0;
330                 public boolean execute(Object JavaDoc o) {
331                     sum += ((Integer JavaDoc)o).intValue();
332                     return true;
333                 }
334             };
335         return new Operation() {
336                 public void theirs() {
337                     int sum = 0;
338                     for (Iterator i = theirSet.iterator(); i.hasNext();) {
339                         sum += ((Integer JavaDoc)i.next()).intValue();
340                     }
341                 }
342
343                 public void ours() {
344                     ourSet.forEach(proc);
345                 }
346
347                 public String JavaDoc toString() {
348                     return "sums a " + theirSet.size() + " element Set of Integer objects. Their approach uses Iterator.hasNext()/next(); ours uses THashSet.forEach(TObjectProcedure)";
349                 }
350
351                 public int getIterationCount() {
352                     return ITERATIONS;
353                 }
354             };
355     }
356
357
358     public static void main (String JavaDoc[] args) {
359         Operation op;
360         Timer t;
361         Reporter reporter;
362
363         reporter = new TextReporter();
364         reporter.start();
365
366         op = getRandomSetContainsOp(); // 10 times
367
t = new Repeater(op);
368         reporter.report(t.run());
369
370         op = getSumSetOperation(); // 10 times
371
t = new Repeater(op);
372         reporter.report(t.run());
373
374         op = getIterationWithHasNextOp(); // 10 times
375
t = new Repeater(op);
376         reporter.report(t.run());
377
378         op = getIterationOp(); // 10 times
379
t = new Repeater(op);
380         reporter.report(t.run());
381
382         op = getLinkedListAddOp(); // 10 times
383
t = new Repeater(op);
384         reporter.report(t.run());
385
386         op = getIntMapPut(); // 10 times
387
t = new Repeater(op);
388         reporter.report(t.run());
389
390         op = getMapPutOp(); // 10 times
391
t = new Repeater(op);
392         reporter.report(t.run());
393
394         op = getContainsOp(); // 10 times
395
t = new Repeater(op);
396         reporter.report(t.run());
397         
398         op = getSetOperation(); // 10 times
399
t = new Repeater(op);
400         reporter.report(t.run());
401
402         reporter.finish();
403     }
404 } // Main
405
Popular Tags