KickJava   Java API By Example, From Geeks To Geeks.

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


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2006, Rob Eden 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.TIntObjectHashMap;
22
23 import java.util.Random JavaDoc;
24
25
26 /**
27  *
28  */

29 public class CompactionBenchmark {
30     public static void main( String JavaDoc[] args ) {
31         float compact_factor;
32         if ( args.length > 0 ) compact_factor = Float.parseFloat( args[ 0 ] );
33         else compact_factor = 0.5f;
34
35         System.out.println( "Compact factor: " + compact_factor );
36
37         int[] primitives = new int[ 1000000 ];
38         for( int i = 0; i < primitives.length; i++ ) {
39             primitives[ i ] = i;
40         }
41
42         Integer JavaDoc[] objects = new Integer JavaDoc[ primitives.length ];
43         for( int i = 0; i < primitives.length; i++ ) {
44             objects[ i ] = new Integer JavaDoc( primitives[ i ] );
45         }
46
47
48         TIntObjectHashMap<Integer JavaDoc> map = new TIntObjectHashMap<Integer JavaDoc>() {
49
50             @Override JavaDoc
51             public void compact() {
52                 super.compact();
53                 System.out.print( "c" );
54             }
55         };
56         map.setAutoCompactionFactor( compact_factor );
57
58         Random JavaDoc rand = new Random JavaDoc();
59
60         final boolean manual_compaction = compact_factor == 0;
61
62         long min = Long.MAX_VALUE;
63         long max = Long.MIN_VALUE;
64         long total = 0;
65         for( int i = 0; i < 50; i++ ) {
66             long time = System.currentTimeMillis();
67
68             runTest( primitives, objects, map, rand );
69
70             if ( manual_compaction ) map.compact();
71
72             time = System.currentTimeMillis() - time;
73
74             System.out.println( time );
75
76             total += time;
77             min = Math.min( time, min );
78             max = Math.max( time, max );
79         }
80
81         System.out.println( "----------------------" );
82         System.out.println( "Avg: " + ( total / 100.0 ) );
83         System.out.println( "Min: " + min );
84         System.out.println( "Max: " + max );
85     }
86
87
88     private static void runTest( int[] primitives, Integer JavaDoc[] objects,
89         TIntObjectHashMap<Integer JavaDoc> map, Random JavaDoc rand ) {
90
91         for( int i = 0; i < 250000; i++ ) {
92             int index = rand.nextInt( primitives.length );
93             map.put( primitives[ index ], objects[ index ] );
94         }
95
96         for( int i = 0; i < 250000; i++ ) {
97             int index = rand.nextInt( primitives.length );
98             map.remove( primitives[ index ] );
99         }
100     }
101 }
102
Popular Tags