1 19 package gnu.trove.benchmark; 20 21 import gnu.trove.TIntObjectHashMap; 22 23 import java.util.Random ; 24 25 26 29 public class CompactionBenchmark { 30 public static void main( String [] 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 [] objects = new Integer [ primitives.length ]; 43 for( int i = 0; i < primitives.length; i++ ) { 44 objects[ i ] = new Integer ( primitives[ i ] ); 45 } 46 47 48 TIntObjectHashMap<Integer > map = new TIntObjectHashMap<Integer >() { 49 50 @Override 51 public void compact() { 52 super.compact(); 53 System.out.print( "c" ); 54 } 55 }; 56 map.setAutoCompactionFactor( compact_factor ); 57 58 Random rand = new Random (); 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 [] objects, 89 TIntObjectHashMap<Integer > map, Random 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 |