1 30 31 32 package org.hsqldb.test; 33 34 import org.hsqldb.lib.DoubleIntIndex; 35 import org.hsqldb.lib.HashSet; 36 import org.hsqldb.lib.IntKeyHashMap; 37 import org.hsqldb.lib.IntKeyIntValueHashMap; 38 import org.hsqldb.lib.IntValueHashMap; 39 import org.hsqldb.lib.StopWatch; 40 41 44 public class TestLibSpeed { 45 46 static final String [][] sNumeric = { 47 { 48 "ABS", "org.hsqldb.Library.abs" 49 }, { 50 "ACOS", "java.lang.Math.acos" 51 }, { 52 "ASIN", "java.lang.Math.asin" 53 }, { 54 "ATAN", "java.lang.Math.atan" 55 }, { 56 "ATAN2", "java.lang.Math.atan2" 57 }, { 58 "CEILING", "java.lang.Math.ceil" 59 }, { 60 "COS", "java.lang.Math.cos" 61 }, { 62 "COT", "org.hsqldb.Library.cot" 63 }, { 64 "DEGREES", "java.lang.Math.toDegrees" 65 }, { 66 "EXP", "java.lang.Math.exp" 67 }, { 68 "FLOOR", "java.lang.Math.floor" 69 }, { 70 "LOG", "java.lang.Math.log" 71 }, { 72 "LOG10", "org.hsqldb.Library.log10" 73 }, { 74 "MOD", "org.hsqldb.Library.mod" 75 }, { 76 "PI", "org.hsqldb.Library.pi" 77 }, { 78 "POWER", "java.lang.Math.pow" 79 }, { 80 "RADIANS", "java.lang.Math.toRadians" 81 }, { 82 "RAND", "java.lang.Math.random" 83 }, { 84 "ROUND", "org.hsqldb.Library.round" 85 }, { 86 "SIGN", "org.hsqldb.Library.sign" 87 }, { 88 "SIN", "java.lang.Math.sin" 89 }, { 90 "SQRT", "java.lang.Math.sqrt" 91 }, { 92 "TAN", "java.lang.Math.tan" 93 }, { 94 "TRUNCATE", "org.hsqldb.Library.truncate" 95 }, { 96 "BITAND", "org.hsqldb.Library.bitand" 97 }, { 98 "BITOR", "org.hsqldb.Library.bitor" 99 }, { 100 "ROUNDMAGIC", "org.hsqldb.Library.roundMagic" 101 } 102 }; 103 static HashSet hashSet = new HashSet(); 104 static DoubleIntIndex doubleIntLookup = 105 new DoubleIntIndex(sNumeric.length, false); 106 static IntKeyIntValueHashMap intKeyIntValueHashLookup = 107 new IntKeyIntValueHashMap(); 108 static IntValueHashMap intValueHashLookup = 109 new IntValueHashMap(sNumeric.length); 110 static IntKeyHashMap intKeyHashLookup = new IntKeyHashMap(); 111 112 static { 113 doubleIntLookup.setKeysSearchTarget(); 114 115 java.util.Random randomgen = new java.util.Random (); 116 int[] row = new int[2]; 117 118 for (int i = 0; i < sNumeric.length; i++) { 119 hashSet.add(sNumeric[i][0]); 120 intKeyIntValueHashLookup.put(randomgen.nextInt(sNumeric.length), 121 i); 122 intKeyHashLookup.put(i, new Integer (i)); 123 doubleIntLookup.add(randomgen.nextInt(sNumeric.length), i); 124 intValueHashLookup.put(sNumeric[i][0], 125 randomgen.nextInt(sNumeric.length)); 126 } 127 } 128 129 static int count = 100000; 130 131 public TestLibSpeed() { 132 133 java.util.Random randomgen = new java.util.Random (); 134 StopWatch sw = new StopWatch(); 135 int dummy = 0; 136 137 System.out.println("set lookup "); 138 139 for (int k = 0; k < 3; k++) { 140 sw.zero(); 141 142 for (int j = 0; j < count; j++) { 143 for (int i = 0; i < sNumeric.length; i++) { 144 int r = randomgen.nextInt(sNumeric.length); 145 146 hashSet.contains(sNumeric[r][0]); 147 148 dummy += r; 149 } 150 } 151 152 System.out.println("HashSet contains " + sw.elapsedTime()); 153 sw.zero(); 154 155 for (int j = 0; j < count; j++) { 156 for (int i = 0; i < sNumeric.length; i++) { 157 int r = randomgen.nextInt(sNumeric.length); 158 159 intKeyIntValueHashLookup.get(r, -1); 160 161 dummy += r; 162 } 163 } 164 165 System.out.println("IntKeyIntValueHashMap Lookup with array " 166 + sw.elapsedTime()); 167 sw.zero(); 168 169 for (int j = 0; j < count; j++) { 170 for (int i = 0; i < sNumeric.length; i++) { 171 int r = randomgen.nextInt(sNumeric.length); 172 173 intKeyHashLookup.get(r); 174 175 dummy += r; 176 } 177 } 178 179 System.out.println("IntKeyHashMap Lookup " + sw.elapsedTime()); 180 sw.zero(); 181 182 for (int j = 0; j < count; j++) { 183 for (int i = 0; i < sNumeric.length; i++) { 184 int r = randomgen.nextInt(sNumeric.length); 185 186 doubleIntLookup.findFirstEqualKeyIndex(r); 187 188 dummy += r; 189 } 190 } 191 192 System.out.println("DoubleIntTable Lookup " + sw.elapsedTime()); 193 sw.zero(); 194 195 for (int j = 0; j < count; j++) { 196 for (int i = 0; i < sNumeric.length; i++) { 197 int r = randomgen.nextInt(sNumeric.length); 198 199 intValueHashLookup.get(sNumeric[r][0], 0); 200 201 dummy += r; 202 } 203 } 204 205 System.out.println("IntKeyIntValueHashMap Lookup " 206 + sw.elapsedTime()); 207 sw.zero(); 208 209 for (int j = 0; j < count; j++) { 210 for (int i = 0; i < sNumeric.length; i++) { 211 int r = randomgen.nextInt(sNumeric.length); 212 213 dummy += r; 214 } 215 } 216 217 System.out.println("emptyOp " + sw.elapsedTime()); 218 sw.zero(); 219 220 for (int j = 0; j < count; j++) { 221 for (int i = 0; i < sNumeric.length; i++) { 222 int r = randomgen.nextInt(sNumeric.length); 223 224 doubleIntLookup.findFirstEqualKeyIndex(r); 225 226 dummy += r; 227 } 228 } 229 230 System.out.println("DoubleIntTable Lookup " + sw.elapsedTime()); 231 sw.zero(); 232 System.out.println("Object Cache Test " + sw.elapsedTime()); 233 } 234 } 235 236 public static void main(String [] argv) { 237 TestLibSpeed ls = new TestLibSpeed(); 238 } 239 } 240 | Popular Tags |