KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > PerfUtil


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution;
10
11 import j2me.util.ArrayList;
12 import j2me.util.HashMap;
13 import j2me.util.HashSet;
14 import j2me.util.Iterator;
15 import j2me.util.LinkedHashMap;
16 import j2me.util.LinkedHashSet;
17 import j2me.util.LinkedList;
18 import j2me.util.RandomAccess;
19
20 import javolution.util.FastList;
21 import javolution.util.FastMap;
22 import javolution.util.FastSet;
23 import javolution.util.FastTable;
24
25 /**
26  * <p> This class holds {@link javolution.util} benchmark.</p>
27  *
28  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
29  * @version 2.0, November 26, 2004
30  */

31 final class PerfUtil extends Javolution implements Runnable JavaDoc {
32
33     private static final int MAX_COLLECTION_SIZE = 10000;
34
35     private static final int ITERATIONS = 100;
36
37     private final Object JavaDoc[] _objects = new Object JavaDoc[MAX_COLLECTION_SIZE];
38
39     /**
40      * Executes benchmark.
41      */

42     public void run() throws JavolutionError {
43
44         println("//////////////////////////////");
45         println("// Package: javolution.util //");
46         println("//////////////////////////////");
47         println("");
48         println("(new) : The collection is created (using the new keyword), populated, then discarded (throw-away collections).");
49         println("(recycled) : The collection is cleared, populated, then reused (static collections or throw-away collections in PoolContext).");
50         println("");
51
52         // Creates objects collection.
53
for (int i = 0; i < MAX_COLLECTION_SIZE; i++) {
54             _objects[i] = new Object JavaDoc();
55         }
56
57         println("-- FastTable versus ArrayList -- ");
58         benchmarkFastTable();
59         benchmarkArrayList();
60
61         println("-- FastList versus LinkedList -- ");
62         benchmarkFastList();
63         benchmarkLinkedList();
64         println("");
65
66         println("-- FastMap versus HashMap --");
67         benchmarkFastMap();
68         benchmarkHashMap();
69         benchmarkLinkedHashMap();
70         println("");
71
72         println("-- FastMap.setShared(true) versus ConcurrentHashMap --");
73         benchmarkSharedFastMap();
74         /*@JVM-1.5+@
75          benchmarkConcurrentHashMap();
76          /**/

77         println("");
78
79         println("-- FastSet versus HashSet --");
80         benchmarkFastSet();
81         benchmarkHashSet();
82         benchmarkLinkedHashSet();
83         println("");
84
85     }
86
87     private void benchmarkFastTable() {
88         FastTable list = new FastTable();
89         println(list.getClass());
90
91         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
92             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
93             print(" Size: " + size);
94
95             print(", add (new): ");
96             for (int n = 0; n < ITERATIONS; n++) {
97                 startTime();
98                 for (int j = 0; j < iterations; j++) {
99                     list = new FastTable();
100                     for (int i = 0; i < size;) {
101                         list.add(_objects[i++]);
102                     }
103                 }
104                 keepBestTime(size * iterations);
105             }
106             print(endTime());
107
108             print(", add (recycled): ");
109             for (int n = 0; n < ITERATIONS; n++) {
110                 startTime();
111                 for (int j = 0; j < iterations; j++) {
112                     list.clear();
113                     for (int i = 0; i < size;) {
114                         list.add(_objects[i++]);
115                     }
116                 }
117                 keepBestTime(size * iterations);
118             }
119             print(endTime());
120
121             print(", iteration (iterator): ");
122             for (int n = 0; n < ITERATIONS; n++) {
123                 startTime();
124                 for (int j = 0; j < iterations; j++) {
125                     for (Iterator i = list.iterator(); i.hasNext();) {
126                         if (i.next() == list)
127                             throw new Error JavaDoc();
128                     }
129                 }
130                 keepBestTime(size * iterations);
131             }
132             print(endTime());
133
134             if (list instanceof RandomAccess) {
135                 print(", get(int): ");
136                 for (int n = 0; n < ITERATIONS; n++) {
137                     startTime();
138                     for (int j = 0; j < iterations; j++) {
139                         for (int i = list.size(); --i > 0;) {
140                             if (list.get(i) == list)
141                                 throw new Error JavaDoc();
142                         }
143                     }
144                     keepBestTime(size * iterations);
145                 }
146                 print(endTime());
147             }
148
149             println("");
150         }
151         println("");
152     }
153
154     private void benchmarkFastList() {
155         FastList list = new FastList();
156         println(list.getClass());
157
158         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
159             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
160             print(" Size: " + size);
161
162             print(", add (new): ");
163             for (int n = 0; n < ITERATIONS; n++) {
164                 startTime();
165                 for (int j = 0; j < iterations; j++) {
166                     list = new FastList();
167                     for (int i = 0; i < size;) {
168                         list.add(_objects[i++]);
169                     }
170                 }
171                 keepBestTime(size * iterations);
172             }
173             print(endTime());
174
175             print(", add (recycled): ");
176             for (int n = 0; n < ITERATIONS; n++) {
177                 startTime();
178                 for (int j = 0; j < iterations; j++) {
179                     list.clear();
180                     for (int i = 0; i < size;) {
181                         list.add(_objects[i++]);
182                     }
183                 }
184                 keepBestTime(size * iterations);
185             }
186             print(endTime());
187
188             print(", iteration (iterator): ");
189             for (int n = 0; n < ITERATIONS; n++) {
190                 startTime();
191                 for (int j = 0; j < iterations; j++) {
192                     for (Iterator i = list.iterator(); i.hasNext();) {
193                         if (i.next() == list)
194                             throw new Error JavaDoc();
195                     }
196                 }
197                 keepBestTime(size * iterations);
198             }
199             print(endTime());
200
201             print(", iteration (node): ");
202             FastList fl = (FastList) list;
203             for (int n = 0; n < ITERATIONS; n++) {
204                 startTime();
205                 for (int j = 0; j < iterations; j++) {
206                     for (FastList.Node nn = (FastList.Node) /*CAST UNNECESSARY WITH JDK1.5*/fl
207                             .head(), end = (FastList.Node) /*CAST UNNECESSARY WITH JDK1.5*/fl
208                             .tail(); (nn = (FastList.Node) /*CAST UNNECESSARY WITH JDK1.5*/nn
209                             .getNext()) != end;) {
210                         if (nn.getValue() == list)
211                             throw new Error JavaDoc();
212                     }
213                 }
214                 keepBestTime(size * iterations);
215             }
216             print(endTime());
217
218             if (list instanceof RandomAccess) {
219                 print(", get(int): ");
220                 for (int n = 0; n < ITERATIONS; n++) {
221                     startTime();
222                     for (int j = 0; j < iterations; j++) {
223                         for (int i = list.size(); --i > 0;) {
224                             if (list.get(i) == list)
225                                 throw new Error JavaDoc();
226                         }
227                     }
228                     keepBestTime(size * iterations);
229                 }
230                 print(endTime());
231             }
232
233             println("");
234         }
235         println("");
236     }
237
238     private void benchmarkArrayList() {
239         ArrayList list = new ArrayList();
240         if (!list.getClass().getName().equals("java.util.ArrayList"))
241             return; // J2ME Target.
242
println(list.getClass());
243
244         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
245             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
246             print(" Size: " + size);
247
248             print(", add (new): ");
249             for (int n = 0; n < ITERATIONS; n++) {
250                 startTime();
251                 for (int j = 0; j < iterations; j++) {
252                     list = new ArrayList();
253                     for (int i = 0; i < size;) {
254                         list.add(_objects[i++]);
255                     }
256                 }
257                 keepBestTime(size * iterations);
258             }
259             print(endTime());
260
261             print(", add (recycled): ");
262             for (int n = 0; n < ITERATIONS; n++) {
263                 startTime();
264                 for (int j = 0; j < iterations; j++) {
265                     list.clear();
266                     for (int i = 0; i < size;) {
267                         list.add(_objects[i++]);
268                     }
269                 }
270                 keepBestTime(size * iterations);
271             }
272             print(endTime());
273
274             print(", iteration (iterator): ");
275             for (int n = 0; n < ITERATIONS; n++) {
276                 startTime();
277                 for (int j = 0; j < iterations; j++) {
278                     for (Iterator i = list.iterator(); i.hasNext();) {
279                         if (i.next() == list)
280                             throw new Error JavaDoc();
281                     }
282                 }
283                 keepBestTime(size * iterations);
284             }
285             print(endTime());
286
287             if (list instanceof RandomAccess) {
288                 print(", get(int): ");
289                 for (int n = 0; n < ITERATIONS; n++) {
290                     startTime();
291                     for (int j = 0; j < iterations; j++) {
292                         for (int i = list.size(); --i > 0;) {
293                             if (list.get(i) == list)
294                                 throw new Error JavaDoc();
295                         }
296                     }
297                     keepBestTime(size * iterations);
298                 }
299                 print(endTime());
300             }
301
302             println("");
303         }
304         println("");
305     }
306
307     private void benchmarkLinkedList() {
308         LinkedList list = new LinkedList();
309         if (!list.getClass().getName().equals("java.util.LinkedList"))
310             return; // J2ME Target.
311
println(list.getClass());
312
313         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
314             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
315             print(" Size: " + size);
316
317             print(", add (new): ");
318             for (int n = 0; n < ITERATIONS; n++) {
319                 startTime();
320                 for (int j = 0; j < iterations; j++) {
321                     list = new LinkedList();
322                     for (int i = 0; i < size;) {
323                         list.add(_objects[i++]);
324                     }
325                 }
326                 keepBestTime(size * iterations);
327             }
328             print(endTime());
329
330             print(", add (recycled): ");
331             for (int n = 0; n < ITERATIONS; n++) {
332                 startTime();
333                 for (int j = 0; j < iterations; j++) {
334                     list.clear();
335                     for (int i = 0; i < size;) {
336                         list.add(_objects[i++]);
337                     }
338                 }
339                 keepBestTime(size * iterations);
340             }
341             print(endTime());
342
343             print(", iteration (iterator): ");
344             for (int n = 0; n < ITERATIONS; n++) {
345                 startTime();
346                 for (int j = 0; j < iterations; j++) {
347                     for (Iterator i = list.iterator(); i.hasNext();) {
348                         if (i.next() == list)
349                             throw new Error JavaDoc();
350                     }
351                 }
352                 keepBestTime(size * iterations);
353             }
354             print(endTime());
355
356             if (list instanceof RandomAccess) {
357                 print(", get(int): ");
358                 for (int n = 0; n < ITERATIONS; n++) {
359                     startTime();
360                     for (int j = 0; j < iterations; j++) {
361                         for (int i = list.size(); --i > 0;) {
362                             if (list.get(i) == list)
363                                 throw new Error JavaDoc();
364                         }
365                     }
366                     keepBestTime(size * iterations);
367                 }
368                 print(endTime());
369             }
370
371             println("");
372         }
373         println("");
374     }
375
376     private void benchmarkFastMap() {
377         FastMap map = new FastMap();
378         println(map.getClass());
379
380         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
381             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
382             print(" Size: " + size);
383
384             print(", put (new): ");
385             for (int n = 0; n < ITERATIONS; n++) {
386                 startTime();
387                 for (int j = 0; j < iterations; j++) {
388                     map = new FastMap();
389                     for (int i = 0; i < size;) {
390                         map.put(_objects[i++], "");
391                     }
392                 }
393                 keepBestTime(size * iterations);
394             }
395             print(endTime());
396
397             print(", put (recycled): ");
398             for (int n = 0; n < ITERATIONS; n++) {
399                 startTime();
400                 for (int j = 0; j < iterations; j++) {
401                     map.clear();
402                     for (int i = 0; i < size;) {
403                         map.put(_objects[i++], "");
404                     }
405                 }
406                 keepBestTime(size * iterations);
407             }
408             print(endTime());
409
410             print(", get: ");
411             for (int n = 0; n < ITERATIONS; n++) {
412                 startTime();
413                 for (int j = 0; j < iterations; j++) {
414                     for (int i = 0; i < size;) {
415                         if (map.get(_objects[i++]) != "")
416                             throw new Error JavaDoc();
417                     }
418                 }
419                 keepBestTime(size * iterations);
420             }
421             print(endTime());
422
423             print(", iteration (iterator): ");
424             for (int n = 0; n < ITERATIONS; n++) {
425                 startTime();
426                 for (int j = 0; j < iterations; j++) {
427                     for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
428                         if (i.next() == map)
429                             throw new Error JavaDoc();
430                     }
431                 }
432                 keepBestTime(size * iterations);
433             }
434             print(endTime());
435
436             print(", iteration (entry): ");
437             for (int n = 0; n < ITERATIONS; n++) {
438                 startTime();
439                 for (int j = 0; j < iterations; j++) {
440                     for (FastMap.Entry e = map.head(), end = map.tail(); (e = (FastMap.Entry) /*CAST UNNECESSARY WITH JDK1.5*/e
441                             .getNext()) != end;) {
442                         if (e.getValue() == map)
443                             throw new Error JavaDoc();
444                     }
445                 }
446                 keepBestTime(size * iterations);
447             }
448             print(endTime());
449
450             println("");
451         }
452         println("");
453
454     }
455
456     private void benchmarkHashMap() {
457         HashMap map = new HashMap();
458         if (!map.getClass().getName().equals("java.util.HashMap"))
459             return; // J2ME Target.
460
println(map.getClass());
461
462         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
463             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
464             print(" Size: " + size);
465
466             print(", put (new): ");
467             for (int n = 0; n < ITERATIONS; n++) {
468                 startTime();
469                 for (int j = 0; j < iterations; j++) {
470                     map = new HashMap();
471                     for (int i = 0; i < size;) {
472                         map.put(_objects[i++], "");
473                     }
474                 }
475                 keepBestTime(size * iterations);
476             }
477             print(endTime());
478
479             print(", put (recycled): ");
480             for (int n = 0; n < ITERATIONS; n++) {
481                 startTime();
482                 for (int j = 0; j < iterations; j++) {
483                     map.clear();
484                     for (int i = 0; i < size;) {
485                         map.put(_objects[i++], "");
486                     }
487                 }
488                 keepBestTime(size * iterations);
489             }
490             print(endTime());
491
492             print(", get: ");
493             for (int n = 0; n < ITERATIONS; n++) {
494                 startTime();
495                 for (int j = 0; j < iterations; j++) {
496                     for (int i = 0; i < size;) {
497                         if (map.get(_objects[i++]) != "")
498                             throw new Error JavaDoc();
499                     }
500                 }
501                 keepBestTime(size * iterations);
502             }
503             print(endTime());
504
505             print(", iteration (iterator): ");
506             for (int n = 0; n < ITERATIONS; n++) {
507                 startTime();
508                 for (int j = 0; j < iterations; j++) {
509                     for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
510                         if (i.next() == map)
511                             throw new Error JavaDoc();
512                     }
513                 }
514                 keepBestTime(size * iterations);
515             }
516             print(endTime());
517
518             println("");
519         }
520         println("");
521     }
522
523     private void benchmarkSharedFastMap() {
524         FastMap map = new FastMap().setShared(true);
525         println("Shared FastMap");
526
527         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
528             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
529             print(" Size: " + size);
530
531             print(", put (new): ");
532             for (int n = 0; n < ITERATIONS; n++) {
533                 startTime();
534                 for (int j = 0; j < iterations; j++) {
535                     map = new FastMap().setShared(true);
536                     for (int i = 0; i < size;) {
537                         map.put(_objects[i++], "");
538                     }
539                 }
540                 keepBestTime(size * iterations);
541             }
542             print(endTime());
543
544             print(", put (recycled): ");
545             for (int n = 0; n < ITERATIONS; n++) {
546                 startTime();
547                 for (int j = 0; j < iterations; j++) {
548                     map.clear();
549                     for (int i = 0; i < size;) {
550                         map.put(_objects[i++], "");
551                     }
552                 }
553                 keepBestTime(size * iterations);
554             }
555             print(endTime());
556
557             print(", get: ");
558             for (int n = 0; n < ITERATIONS; n++) {
559                 startTime();
560                 for (int j = 0; j < iterations; j++) {
561                     for (int i = 0; i < size;) {
562                         if (map.get(_objects[i++]) != "")
563                             throw new Error JavaDoc();
564                     }
565                 }
566                 keepBestTime(size * iterations);
567             }
568             print(endTime());
569
570             print(", iteration (iterator): ");
571             for (int n = 0; n < ITERATIONS; n++) {
572                 startTime();
573                 for (int j = 0; j < iterations; j++) {
574                     for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
575                         if (i.next() == map)
576                             throw new Error JavaDoc();
577                     }
578                 }
579                 keepBestTime(size * iterations);
580             }
581             print(endTime());
582
583             print(", iteration (entry): ");
584             for (int n = 0; n < ITERATIONS; n++) {
585                 startTime();
586                 for (int j = 0; j < iterations; j++) {
587                     for (FastMap.Entry e = map.head(), end = map.tail(); (e = (FastMap.Entry) /*CAST UNNECESSARY WITH JDK1.5*/e
588                             .getNext()) != end;) {
589                         if (e.getValue() == map)
590                             throw new Error JavaDoc();
591                     }
592                 }
593                 keepBestTime(size * iterations);
594             }
595             print(endTime());
596
597             println("");
598         }
599         println("");
600
601     }
602
603     /*@JVM-1.5+@
604      private void benchmarkConcurrentHashMap() {
605      java.util.concurrent.ConcurrentHashMap map = new java.util.concurrent.ConcurrentHashMap();
606      println(map.getClass());
607
608      for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
609      final int iterations = 10 * MAX_COLLECTION_SIZE / size;
610      print(" Size: " + size);
611
612      print(", put (new): ");
613      for (int n=0; n < 10; n++) { startTime();
614      for (int j = 0; j < iterations; j++) {
615      map = new java.util.concurrent.ConcurrentHashMap();
616      for (int i = 0; i < size;) {
617      map.put(_objects[i++], "");
618      }
619      }
620      keepBestTime(size * iterations);
621      } print(endTime());
622
623      print(", put (recycled): ");
624      for (int n=0; n < 10; n++) { startTime();
625      for (int j = 0; j < iterations; j++) {
626      map.clear();
627      for (int i = 0; i < size;) {
628      map.put(_objects[i++], "");
629      }
630      }
631      keepBestTime(size * iterations);
632      } print(endTime());
633
634      print(", get: ");
635      for (int n=0; n < 10; n++) { startTime();
636      for (int j = 0; j < iterations; j++) {
637      for (int i = 0; i < size;) {
638      if (map.get(_objects[i++]) != "")
639      throw new Error();
640      }
641      }
642      keepBestTime(size * iterations);
643      } print(endTime());
644
645      print(", iteration (iterator): ");
646      for (int n=0; n < 10; n++) { startTime();
647      for (int j = 0; j < iterations; j++) {
648      for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
649      if (i.next() == map)
650      throw new Error();
651      }
652      }
653      keepBestTime(size * iterations);
654      } print(endTime());
655
656      println("");
657      }
658      println("");
659      }
660      /**/

661
662     private void benchmarkLinkedHashMap() {
663         LinkedHashMap map = new LinkedHashMap();
664         if (!map.getClass().getName().equals("java.util.LinkedHashMap"))
665             return; // J2ME Target.
666
println(map.getClass());
667
668         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
669             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
670             print(" Size: " + size);
671
672             print(", put (new): ");
673             for (int n = 0; n < ITERATIONS; n++) {
674                 startTime();
675                 for (int j = 0; j < iterations; j++) {
676                     map = new LinkedHashMap();
677                     for (int i = 0; i < size;) {
678                         map.put(_objects[i++], "");
679                     }
680                 }
681                 keepBestTime(size * iterations);
682             }
683             print(endTime());
684
685             print(", put (recycled): ");
686             for (int n = 0; n < ITERATIONS; n++) {
687                 startTime();
688                 for (int j = 0; j < iterations; j++) {
689                     map.clear();
690                     for (int i = 0; i < size;) {
691                         map.put(_objects[i++], "");
692                     }
693                 }
694                 keepBestTime(size * iterations);
695             }
696             print(endTime());
697
698             print(", get: ");
699             for (int n = 0; n < ITERATIONS; n++) {
700                 startTime();
701                 for (int j = 0; j < iterations; j++) {
702                     for (int i = 0; i < size;) {
703                         if (map.get(_objects[i++]) != "")
704                             throw new Error JavaDoc();
705                     }
706                 }
707                 keepBestTime(size * iterations);
708             }
709             print(endTime());
710
711             print(", iteration (iterator): ");
712             for (int n = 0; n < ITERATIONS; n++) {
713                 startTime();
714                 for (int j = 0; j < iterations; j++) {
715                     for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
716                         if (i.next() == map)
717                             throw new Error JavaDoc();
718                     }
719                 }
720                 keepBestTime(size * iterations);
721             }
722             print(endTime());
723
724             println("");
725         }
726         println("");
727     }
728
729     private void benchmarkFastSet() {
730         FastSet set = new FastSet();
731         println(set.getClass());
732
733         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
734             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
735             print(" Size: " + size);
736
737             print(", add (new): ");
738             for (int n = 0; n < ITERATIONS; n++) {
739                 startTime();
740                 for (int j = 0; j < iterations; j++) {
741                     set = new FastSet();
742                     for (int i = 0; i < size;) {
743                         set.add(_objects[i++]);
744                     }
745                 }
746                 keepBestTime(size * iterations);
747             }
748             print(endTime());
749
750             print(", add (recycled): ");
751             for (int n = 0; n < ITERATIONS; n++) {
752                 startTime();
753                 for (int j = 0; j < iterations; j++) {
754                     set.clear();
755                     for (int i = 0; i < size;) {
756                         set.add(_objects[i++]);
757                     }
758                 }
759                 keepBestTime(size * iterations);
760             }
761             print(endTime());
762
763             print(", contain: ");
764             for (int n = 0; n < ITERATIONS; n++) {
765                 startTime();
766                 for (int j = 0; j < iterations; j++) {
767                     for (int i = 0; i < size;) {
768                         if (!set.contains(_objects[i++]))
769                             throw new Error JavaDoc();
770                     }
771                 }
772                 keepBestTime(size * iterations);
773             }
774             print(endTime());
775
776             print(", iteration (iterator): ");
777             for (int n = 0; n < ITERATIONS; n++) {
778                 startTime();
779                 for (int j = 0; j < iterations; j++) {
780                     for (Iterator i = set.iterator(); i.hasNext();) {
781                         if (i.next() == set)
782                             throw new Error JavaDoc();
783                     }
784                 }
785                 keepBestTime(size * iterations);
786             }
787             print(endTime());
788
789             print(", iteration (record): ");
790             for (int n = 0; n < ITERATIONS; n++) {
791                 startTime();
792                 for (int j = 0; j < iterations; j++) {
793                     for (FastSet.Record r = set.head(), end = set.tail(); (r = r
794                             .getNext()) != end;) {
795                         if (set.valueOf(r) == set)
796                             throw new Error JavaDoc();
797                     }
798                 }
799                 keepBestTime(size * iterations);
800             }
801             print(endTime());
802
803             println("");
804         }
805         println("");
806
807     }
808
809     private void benchmarkHashSet() {
810         HashSet set = new HashSet();
811         if (!set.getClass().getName().equals("java.util.HashSet"))
812             return; // J2ME Target.
813
println(set.getClass());
814
815         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
816             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
817             print(" Size: " + size);
818
819             print(", add (new): ");
820             for (int n = 0; n < ITERATIONS; n++) {
821                 startTime();
822                 for (int j = 0; j < iterations; j++) {
823                     set = new HashSet();
824                     for (int i = 0; i < size;) {
825                         set.add(_objects[i++]);
826                     }
827                 }
828                 keepBestTime(size * iterations);
829             }
830             print(endTime());
831
832             print(", add (recycled): ");
833             for (int n = 0; n < ITERATIONS; n++) {
834                 startTime();
835                 for (int j = 0; j < iterations; j++) {
836                     set.clear();
837                     for (int i = 0; i < size;) {
838                         set.add(_objects[i++]);
839                     }
840                 }
841                 keepBestTime(size * iterations);
842             }
843             print(endTime());
844
845             print(", contain: ");
846             for (int n = 0; n < ITERATIONS; n++) {
847                 startTime();
848                 for (int j = 0; j < iterations; j++) {
849                     for (int i = 0; i < size;) {
850                         if (!set.contains(_objects[i++]))
851                             throw new Error JavaDoc();
852                     }
853                 }
854                 keepBestTime(size * iterations);
855             }
856             print(endTime());
857
858             print(", iteration (iterator): ");
859             for (int n = 0; n < ITERATIONS; n++) {
860                 startTime();
861                 for (int j = 0; j < iterations; j++) {
862                     for (Iterator i = set.iterator(); i.hasNext();) {
863                         if (i.next() == set)
864                             throw new Error JavaDoc();
865                     }
866                 }
867                 keepBestTime(size * iterations);
868             }
869             print(endTime());
870
871             println("");
872         }
873         println("");
874
875     }
876
877     private void benchmarkLinkedHashSet() {
878         LinkedHashSet set = new LinkedHashSet();
879         if (!set.getClass().getName().equals("java.util.LinkedHashSet"))
880             return; // J2ME Target.
881
println(set.getClass());
882
883         for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) {
884             final int iterations = 10 * MAX_COLLECTION_SIZE / size;
885             print(" Size: " + size);
886
887             print(", add (new): ");
888             for (int n = 0; n < ITERATIONS; n++) {
889                 startTime();
890                 for (int j = 0; j < iterations; j++) {
891                     set = new LinkedHashSet();
892                     for (int i = 0; i < size;) {
893                         set.add(_objects[i++]);
894                     }
895                 }
896                 keepBestTime(size * iterations);
897             }
898             print(endTime());
899
900             print(", add (recycled): ");
901             for (int n = 0; n < ITERATIONS; n++) {
902                 startTime();
903                 for (int j = 0; j < iterations; j++) {
904                     set.clear();
905                     for (int i = 0; i < size;) {
906                         set.add(_objects[i++]);
907                     }
908                 }
909                 keepBestTime(size * iterations);
910             }
911             print(endTime());
912
913             print(", contain: ");
914             for (int n = 0; n < ITERATIONS; n++) {
915                 startTime();
916                 for (int j = 0; j < iterations; j++) {
917                     for (int i = 0; i < size;) {
918                         if (!set.contains(_objects[i++]))
919                             throw new Error JavaDoc();
920                     }
921                 }
922                 keepBestTime(size * iterations);
923             }
924             print(endTime());
925
926             print(", iteration (iterator): ");
927             for (int n = 0; n < ITERATIONS; n++) {
928                 startTime();
929                 for (int j = 0; j < iterations; j++) {
930                     for (Iterator i = set.iterator(); i.hasNext();) {
931                         if (i.next() == set)
932                             throw new Error JavaDoc();
933                     }
934                 }
935                 keepBestTime(size * iterations);
936             }
937             print(endTime());
938
939             println("");
940         }
941         println("");
942
943     }
944 }
945
Popular Tags