1 22 23 import java.util.*; 24 25 import com.sosnoski.util.array.*; 26 27 36 37 public class TimeFills 38 { 39 42 43 public static final int DEFAULT_PASS_COUNT = 1; 44 45 48 49 public static final int DELAY_BETWEEN_TESTS = 1000; 50 51 54 55 public static final int GARBAGE_COLLECT_DELAY = 1000; 56 57 60 61 public static final int INITIAL_COLLECTION_SIZE = 10; 62 63 66 67 public static final int SEQUENCE_ADD = 11; 68 69 72 73 public static final int SEQUENCE_MULTIPLY = 43; 74 75 78 79 private int lastValue; 80 81 84 85 private long startTime; 86 87 90 91 private int checkResult; 92 93 96 97 private boolean printFlag; 98 99 105 106 protected int nextInSequence() { 107 lastValue = (lastValue + SEQUENCE_ADD) * SEQUENCE_MULTIPLY; 108 return lastValue; 109 } 110 111 118 119 public int fillCollection(int count, int[] collect) { 120 for (int i = 0; i < count; i++) { 121 collect[i] = nextInSequence(); 122 } 123 return collect[count - 1]; 124 } 125 126 public int fillCollection(int count, DirectIntArray collect) { 127 collect.clear(); 128 for (int i = 0; i < count; i++) { 129 collect.add(nextInSequence()); 130 } 131 return collect.get(count - 1); 132 } 133 134 public int fillCollection(int count, IntArray collect) { 135 collect.clear(); 136 for (int i = 0; i < count; i++) { 137 collect.add(nextInSequence()); 138 } 139 return collect.get(count - 1); 140 } 141 142 public int fillCollection(int count, IntegerArray collect) { 143 collect.clear(); 144 for (int i = 0; i < count; i++) { 145 collect.add(new Integer (nextInSequence())); 146 } 147 return collect.get(count - 1).intValue(); 148 } 149 150 public int fillCollection(int count, ObjectArray collect) { 151 collect.clear(); 152 for (int i = 0; i < count; i++) { 153 collect.add(new Integer (nextInSequence())); 154 } 155 return ((Integer )collect.get(count - 1)).intValue(); 156 } 157 158 public int fillCollection(int count, ArrayList collect) { 159 collect.clear(); 160 for (int i = 0; i < count; i++) { 161 collect.add(new Integer (nextInSequence())); 162 } 163 return ((Integer )collect.get(count - 1)).intValue(); 164 } 165 166 public int fillCollection(int count, Vector collect) { 167 collect.clear(); 168 for (int i = 0; i < count; i++) { 169 collect.addElement(new Integer (nextInSequence())); 170 } 171 return ((Integer )collect.elementAt(count - 1)).intValue(); 172 } 173 174 177 178 protected final void startTimer() { 179 startTime = System.currentTimeMillis(); 180 } 181 182 186 187 protected final void cleanMemory() { 188 Runtime rt = Runtime.getRuntime(); 189 rt.gc(); 190 try { 191 Thread.sleep(GARBAGE_COLLECT_DELAY); 192 } catch (InterruptedException ex) {} 193 } 194 195 204 205 protected void printTestTime(int result, String text) { 206 if (printFlag) { 207 long time = System.currentTimeMillis() - startTime; 208 System.out.println("Ran " + text + " add test in " + time + " ms."); 209 if (result != checkResult) { 210 System.out.println(" Error: test result " + result + 211 " does not match check value of " + checkResult); 212 } 213 } 214 } 215 216 226 227 public int runAllTests(int count, int repeats, boolean print) { 228 229 printFlag = print; 231 if (print) { 232 System.out.println("Running tests adding " + count + 233 " values to each collection\n with " + repeats + 234 " cycles for each collection type."); 235 } 236 237 int reset = lastValue; 239 int sum = 0; 240 int result = 0; 241 cleanMemory(); 242 startTimer(); 243 for (int i = 0; i < repeats; i++) { 244 int[] acoll = new int[count]; 245 result += fillCollection(count, acoll); 246 } 247 checkResult = result; 248 printTestTime(result, "int[]"); 249 sum += result; 250 lastValue = reset; 251 result = 0; 252 cleanMemory(); 253 startTimer(); 254 for (int i = 0; i < repeats; i++) { 255 DirectIntArray idcoll = new DirectIntArray(INITIAL_COLLECTION_SIZE); 256 result += fillCollection(count, idcoll); 257 } 258 printTestTime(result, "DirectIntArray"); 259 sum += result; 260 lastValue = reset; 261 result = 0; 262 cleanMemory(); 263 startTimer(); 264 for (int i = 0; i < repeats; i++) { 265 IntArray iacoll = new IntArray(INITIAL_COLLECTION_SIZE); 266 result += fillCollection(count, iacoll); 267 } 268 printTestTime(result, "IntArray"); 269 sum += result; 270 lastValue = reset; 271 result = 0; 272 cleanMemory(); 273 startTimer(); 274 for (int i = 0; i < repeats; i++) { 275 IntegerArray ioacoll = new IntegerArray(INITIAL_COLLECTION_SIZE); 276 result += fillCollection(count, ioacoll); 277 } 278 printTestTime(result, "IntegerArray"); 279 sum += result; 280 lastValue = reset; 281 result = 0; 282 cleanMemory(); 283 startTimer(); 284 for (int i = 0; i < repeats; i++) { 285 ObjectArray oacoll = new ObjectArray(INITIAL_COLLECTION_SIZE); 286 result += fillCollection(count, oacoll); 287 } 288 printTestTime(result, "ObjectArray of Integer"); 289 sum += result; 290 lastValue = reset; 291 result = 0; 292 cleanMemory(); 293 startTimer(); 294 for (int i = 0; i < repeats; i++) { 295 ArrayList alcoll = new ArrayList(INITIAL_COLLECTION_SIZE); 296 result += fillCollection(count, alcoll); 297 } 298 printTestTime(result, "ArrayList of Integer"); 299 sum += result; 300 lastValue = reset; 301 result = 0; 302 cleanMemory(); 303 startTimer(); 304 for (int i = 0; i < repeats; i++) { 305 Vector vcoll = new Vector(INITIAL_COLLECTION_SIZE); 306 result += fillCollection(count, vcoll); 307 } 308 printTestTime(result, "Vector of Integer"); 309 return sum += result; 310 } 311 312 318 319 public static void main(String [] argv) { 320 if (argv.length > 1) { 321 322 int count = Integer.parseInt(argv[0]); 324 int repeats = Integer.parseInt(argv[1]); 325 int passes = 0; 326 if (argv.length > 2) { 327 passes = Integer.parseInt(argv[2]); 328 } 329 330 TimeFills inst = new TimeFills(); 332 333 System.out.println("Java version " + System.getProperty("java.version")); 335 String text = System.getProperty("java.vm.name"); 336 int lines = 1; 337 if (text != null) { 338 System.out.println(text); 339 lines++; 340 } 341 text = System.getProperty("java.vm.version"); 342 if (text != null) { 343 System.out.println(text); 344 lines++; 345 } 346 text = System.getProperty("java.vm.vendor"); 347 if (text == null) { 348 text = System.getProperty("java.vendor"); 349 } 350 System.out.println(text); 351 352 while (++lines < 5) { 354 System.out.println(""); 355 } 356 357 int sum = 0; 359 if (passes == 0) { 360 System.out.println("Running initialization pass..."); 361 inst.runAllTests(count / 5, repeats, false); 362 try { 363 Thread.sleep(DELAY_BETWEEN_TESTS); 364 } catch (InterruptedException ex) {} 365 sum += inst.runAllTests(count, repeats, true); 366 } else { 367 for (int i = 0; i < passes; i++) { 368 sum += inst.runAllTests(count, repeats, true); 369 if (i < passes) { 370 try { 371 Thread.sleep(DELAY_BETWEEN_TESTS); 372 } catch (InterruptedException ex) {} 373 } 374 } 375 } 376 System.out.println("Check result value " + inst.checkResult); 377 378 } else { 379 System.out.println("Requires parameters:\n" + 380 " count - the number of values to be added to each collection\n" + 381 " repeats - the number of times each type of collection is built\n" + 382 " [passes] - number of test passes to run (default is single printed\n" + 383 " pass after initialization pass with one-fifth the loops\n"); 384 } 385 } 386 } 387 | Popular Tags |