1 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 31 final class Perf_Util extends Javolution implements Runnable { 32 33 private static final int MAX_COLLECTION_SIZE = 10000; 34 35 private final Object[] _objects = new Object[MAX_COLLECTION_SIZE]; 36 37 40 public void run() throws JavolutionError { 41 42 println("//////////////////////////////"); 43 println("// Package: javolution.util //"); 44 println("//////////////////////////////"); 45 println(""); 46 println("(new) : The collection is created (using the new keyword), populated, then discarded (throw-away collections)."); 47 println("(recycled) : The collection is cleared, populated, then reused (static collections or throw-away collections in PoolContext)."); 48 println(""); 49 50 for (int i = 0; i < MAX_COLLECTION_SIZE; i++) { 52 _objects[i] = new Object(); 53 } 54 55 println("-- FastTable versus ArrayList -- "); 56 setOutputStream(null); benchmarkFastTable(); 58 setOutputStream(System.out); 59 benchmarkFastTable(); 60 setOutputStream(null); benchmarkArrayList(); 62 setOutputStream(System.out); 63 benchmarkArrayList(); 64 65 println("-- FastList versus LinkedList -- "); 66 setOutputStream(null); benchmarkFastList(); 68 setOutputStream(System.out); 69 benchmarkFastList(); 70 setOutputStream(null); benchmarkLinkedList(); 72 setOutputStream(System.out); 73 benchmarkLinkedList(); 74 println(""); 75 76 println("-- FastMap versus HashMap --"); 77 setOutputStream(null); benchmarkFastMap(); 79 setOutputStream(System.out); 80 benchmarkFastMap(); 81 setOutputStream(null); benchmarkHashMap(); 83 setOutputStream(System.out); 84 benchmarkHashMap(); 85 setOutputStream(null); benchmarkLinkedHashMap(); 87 setOutputStream(System.out); 88 benchmarkLinkedHashMap(); 89 println(""); 90 91 println("-- FastSet versus HashSet --"); 92 setOutputStream(null); benchmarkFastSet(); 94 setOutputStream(System.out); 95 benchmarkFastSet(); 96 setOutputStream(null); benchmarkHashSet(); 98 setOutputStream(System.out); 99 benchmarkHashSet(); 100 setOutputStream(null); benchmarkLinkedHashSet(); 102 setOutputStream(System.out); 103 benchmarkLinkedHashSet(); 104 println(""); 105 106 } 107 108 private void benchmarkFastTable() { 109 FastTable list = new FastTable(); 110 println(list.getClass()); 111 112 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 113 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 114 print(" Size: " + size); 115 116 print(", add (new): "); 117 startTime(); 118 for (int j = 0; j < nbrIterations; j++) { 119 list = new FastTable(); 120 for (int i = 0; i < size;) { 121 list.add(_objects[i++]); 122 } 123 } 124 print(endTime(nbrIterations * size)); 125 126 print(", add (recycled): "); 127 startTime(); 128 for (int j = 0; j < nbrIterations; j++) { 129 list.clear(); 130 for (int i = 0; i < size;) { 131 list.add(_objects[i++]); 132 } 133 } 134 print(endTime(nbrIterations * size)); 135 136 print(", iteration (iterator): "); 137 startTime(); 138 for (int j = 0; j < nbrIterations * 10; j++) { 139 for (Iterator i = list.iterator(); i.hasNext();) { 140 if (i.next() == list) 141 throw new Error(); 142 } 143 } 144 print(endTime(nbrIterations * size * 10)); 145 146 if (list instanceof RandomAccess) { 147 print(", get(int): "); 148 startTime(); 149 for (int j = 0; j < nbrIterations * 10; j++) { 150 for (int i = list.size(); --i > 0;) { 151 if (list.get(i) == list) 152 throw new Error(); 153 } 154 } 155 print(endTime(nbrIterations * size * 10)); 156 } 157 158 println(""); 159 } 160 println(""); 161 } 162 163 private void benchmarkFastList() { 164 FastList list = new FastList(); 165 println(list.getClass()); 166 167 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 168 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 169 print(" Size: " + size); 170 171 print(", add (new): "); 172 startTime(); 173 for (int j = 0; j < nbrIterations; j++) { 174 list = new FastList(); 175 for (int i = 0; i < size;) { 176 list.add(_objects[i++]); 177 } 178 } 179 print(endTime(nbrIterations * size)); 180 181 print(", add (recycled): "); 182 startTime(); 183 for (int j = 0; j < nbrIterations; j++) { 184 list.clear(); 185 for (int i = 0; i < size;) { 186 list.add(_objects[i++]); 187 } 188 } 189 print(endTime(nbrIterations * size)); 190 191 print(", iteration (iterator): "); 192 startTime(); 193 for (int j = 0; j < nbrIterations * 10; j++) { 194 for (Iterator i = list.iterator(); i.hasNext();) { 195 if (i.next() == list) 196 throw new Error(); 197 } 198 } 199 print(endTime(nbrIterations * size * 10)); 200 201 print(", iteration (node): "); 202 FastList fl = (FastList) list; 203 startTime(); 204 for (int j = 0; j < nbrIterations * 10; j++) { 205 for (FastList.Node n = fl.headNode(), end = fl.tailNode(); (n = n 206 .getNextNode()) != end;) { 207 if (n.getValue() == list) 208 throw new Error(); 209 } 210 } 211 print(endTime(nbrIterations * size * 10)); 212 213 if (list instanceof RandomAccess) { 214 print(", get(int): "); 215 startTime(); 216 for (int j = 0; j < nbrIterations * 10; j++) { 217 for (int i = list.size(); --i > 0;) { 218 if (list.get(i) == list) 219 throw new Error(); 220 } 221 } 222 print(endTime(nbrIterations * size * 10)); 223 } 224 225 println(""); 226 } 227 println(""); 228 } 229 230 private void benchmarkArrayList() { 231 ArrayList list = new ArrayList(); 232 if (!list.getClass().getName().equals("java.util.ArrayList")) 233 return; println(list.getClass()); 235 236 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 237 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 238 print(" Size: " + size); 239 240 print(", add (new): "); 241 startTime(); 242 for (int j = 0; j < nbrIterations; j++) { 243 list = new ArrayList(); 244 for (int i = 0; i < size;) { 245 list.add(_objects[i++]); 246 } 247 } 248 print(endTime(nbrIterations * size)); 249 250 print(", add (recycled): "); 251 startTime(); 252 for (int j = 0; j < nbrIterations; j++) { 253 list.clear(); 254 for (int i = 0; i < size;) { 255 list.add(_objects[i++]); 256 } 257 } 258 print(endTime(nbrIterations * size)); 259 260 print(", iteration (iterator): "); 261 startTime(); 262 for (int j = 0; j < nbrIterations * 10; j++) { 263 for (Iterator i = list.iterator(); i.hasNext();) { 264 if (i.next() == list) 265 throw new Error(); 266 } 267 } 268 print(endTime(nbrIterations * size * 10)); 269 270 if (list instanceof RandomAccess) { 271 print(", get(int): "); 272 startTime(); 273 for (int j = 0; j < nbrIterations * 10; j++) { 274 for (int i = list.size(); --i > 0;) { 275 if (list.get(i) == list) 276 throw new Error(); 277 } 278 } 279 print(endTime(nbrIterations * size * 10)); 280 } 281 282 println(""); 283 } 284 println(""); 285 } 286 287 private void benchmarkLinkedList() { 288 LinkedList list = new LinkedList(); 289 if (!list.getClass().getName().equals("java.util.LinkedList")) 290 return; println(list.getClass()); 292 293 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 294 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 295 print(" Size: " + size); 296 297 print(", add (new): "); 298 startTime(); 299 for (int j = 0; j < nbrIterations; j++) { 300 list = new LinkedList(); 301 for (int i = 0; i < size;) { 302 list.add(_objects[i++]); 303 } 304 } 305 print(endTime(nbrIterations * size)); 306 307 print(", add (recycled): "); 308 startTime(); 309 for (int j = 0; j < nbrIterations; j++) { 310 list.clear(); 311 for (int i = 0; i < size;) { 312 list.add(_objects[i++]); 313 } 314 } 315 print(endTime(nbrIterations * size)); 316 317 print(", iteration (iterator): "); 318 startTime(); 319 for (int j = 0; j < nbrIterations * 10; j++) { 320 for (Iterator i = list.iterator(); i.hasNext();) { 321 if (i.next() == list) 322 throw new Error(); 323 } 324 } 325 print(endTime(nbrIterations * size * 10)); 326 327 if (list instanceof RandomAccess) { 328 print(", get(int): "); 329 startTime(); 330 for (int j = 0; j < nbrIterations * 10; j++) { 331 for (int i = list.size(); --i > 0;) { 332 if (list.get(i) == list) 333 throw new Error(); 334 } 335 } 336 print(endTime(nbrIterations * size * 10)); 337 } 338 339 println(""); 340 } 341 println(""); 342 } 343 344 private void benchmarkFastMap() { 345 FastMap map = new FastMap(); 346 println(map.getClass()); 347 348 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 349 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 350 print(" Size: " + size); 351 352 print(", put (new): "); 353 startTime(); 354 for (int j = 0; j < nbrIterations; j++) { 355 map = new FastMap(); 356 for (int i = 0; i < size;) { 357 map.put(_objects[i++], null); 358 } 359 } 360 print(endTime(nbrIterations * size)); 361 362 print(", put (recycled): "); 363 startTime(); 364 for (int j = 0; j < nbrIterations; j++) { 365 map.clear(); 366 for (int i = 0; i < size;) { 367 map.put(_objects[i++], null); 368 } 369 } 370 print(endTime(nbrIterations * size)); 371 372 print(", get: "); 373 startTime(); 374 for (int j = 0; j < nbrIterations; j++) { 375 for (int i = 0; i < size;) { 376 if (map.get(_objects[i++]) == map) 377 throw new Error(); 378 } 379 } 380 print(endTime(nbrIterations * size)); 381 382 print(", iteration (iterator): "); 383 startTime(); 384 for (int j = 0; j < nbrIterations * 10; j++) { 385 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 386 if (i.next() == map) 387 throw new Error(); 388 } 389 } 390 print(endTime(nbrIterations * size * 10)); 391 392 print(", iteration (entry): "); 393 startTime(); 394 for (int j = 0; j < nbrIterations * 10; j++) { 395 for (FastMap.Entry e = map.headEntry(), end = map.tailEntry(); (e = e 396 .getNextEntry()) != end;) { 397 if (e.getValue() == map) 398 throw new Error(); 399 } 400 } 401 print(endTime(nbrIterations * size * 10)); 402 403 println(""); 404 } 405 println(""); 406 407 } 408 409 private void benchmarkHashMap() { 410 HashMap map = new HashMap(); 411 if (!map.getClass().getName().equals("java.util.HashMap")) 412 return; println(map.getClass()); 414 415 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 416 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 417 print(" Size: " + size); 418 419 print(", put (new): "); 420 startTime(); 421 for (int j = 0; j < nbrIterations; j++) { 422 map = new HashMap(); 423 for (int i = 0; i < size;) { 424 map.put(_objects[i++], null); 425 } 426 } 427 print(endTime(nbrIterations * size)); 428 429 print(", put (recycled): "); 430 startTime(); 431 for (int j = 0; j < nbrIterations; j++) { 432 map.clear(); 433 for (int i = 0; i < size;) { 434 map.put(_objects[i++], null); 435 } 436 } 437 print(endTime(nbrIterations * size)); 438 439 print(", get: "); 440 startTime(); 441 for (int j = 0; j < nbrIterations; j++) { 442 for (int i = 0; i < size;) { 443 if (map.get(_objects[i++]) == map) 444 throw new Error(); 445 } 446 } 447 print(endTime(nbrIterations * size)); 448 449 print(", iteration (iterator): "); 450 startTime(); 451 for (int j = 0; j < nbrIterations * 10; j++) { 452 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 453 if (i.next() == map) 454 throw new Error(); 455 } 456 } 457 print(endTime(nbrIterations * size * 10)); 458 459 println(""); 460 } 461 println(""); 462 } 463 464 private void benchmarkLinkedHashMap() { 465 LinkedHashMap map = new LinkedHashMap(); 466 if (!map.getClass().getName().equals("java.util.LinkedHashMap")) 467 return; println(map.getClass()); 469 470 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 471 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 472 print(" Size: " + size); 473 474 print(", put (new): "); 475 startTime(); 476 for (int j = 0; j < nbrIterations; j++) { 477 map = new LinkedHashMap(); 478 for (int i = 0; i < size;) { 479 map.put(_objects[i++], null); 480 } 481 } 482 print(endTime(nbrIterations * size)); 483 484 print(", put (recycled): "); 485 startTime(); 486 for (int j = 0; j < nbrIterations; j++) { 487 map.clear(); 488 for (int i = 0; i < size;) { 489 map.put(_objects[i++], null); 490 } 491 } 492 print(endTime(nbrIterations * size)); 493 494 print(", get: "); 495 startTime(); 496 for (int j = 0; j < nbrIterations; j++) { 497 for (int i = 0; i < size;) { 498 if (map.get(_objects[i++]) == map) 499 throw new Error(); 500 } 501 } 502 print(endTime(nbrIterations * size)); 503 504 print(", iteration (iterator): "); 505 startTime(); 506 for (int j = 0; j < nbrIterations * 10; j++) { 507 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 508 if (i.next() == map) 509 throw new Error(); 510 } 511 } 512 print(endTime(nbrIterations * size * 10)); 513 514 println(""); 515 } 516 println(""); 517 } 518 519 private void benchmarkFastSet() { 520 FastSet set = new FastSet(); 521 println(set.getClass()); 522 523 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 524 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 525 print(" Size: " + size); 526 527 print(", add (new): "); 528 startTime(); 529 for (int j = 0; j < nbrIterations; j++) { 530 set = new FastSet(); 531 for (int i = 0; i < size;) { 532 set.add(_objects[i++]); 533 } 534 } 535 print(endTime(nbrIterations * size)); 536 537 print(", add (recycled): "); 538 startTime(); 539 for (int j = 0; j < nbrIterations; j++) { 540 set.clear(); 541 for (int i = 0; i < size;) { 542 set.add(_objects[i++]); 543 } 544 } 545 print(endTime(nbrIterations * size)); 546 547 print(", contain: "); 548 startTime(); 549 for (int j = 0; j < nbrIterations; j++) { 550 for (int i = 0; i < size;) { 551 if (!set.contains(_objects[i++])) 552 throw new Error(); 553 } 554 } 555 print(endTime(nbrIterations * size)); 556 557 print(", iteration (iterator): "); 558 startTime(); 559 for (int j = 0; j < nbrIterations * 10; j++) { 560 for (Iterator i = set.iterator(); i.hasNext();) { 561 if (i.next() == set) 562 throw new Error(); 563 } 564 } 565 print(endTime(nbrIterations * size * 10)); 566 567 print(", iteration (record): "); 568 startTime(); 569 for (int j = 0; j < nbrIterations * 10; j++) { 570 for (FastSet.Record r = set.headRecord(), end = set 571 .tailRecord(); (r = r.getNextRecord()) != end;) { 572 if (set.valueOf(r) == set) 573 throw new Error(); 574 } 575 } 576 print(endTime(nbrIterations * size * 10)); 577 578 println(""); 579 } 580 println(""); 581 582 } 583 584 private void benchmarkHashSet() { 585 HashSet set = new HashSet(); 586 if (!set.getClass().getName().equals("java.util.HashSet")) 587 return; println(set.getClass()); 589 590 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 591 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 592 print(" Size: " + size); 593 594 print(", add (new): "); 595 startTime(); 596 for (int j = 0; j < nbrIterations; j++) { 597 set = new HashSet(); 598 for (int i = 0; i < size;) { 599 set.add(_objects[i++]); 600 } 601 } 602 print(endTime(nbrIterations * size)); 603 604 print(", add (recycled): "); 605 startTime(); 606 for (int j = 0; j < nbrIterations; j++) { 607 set.clear(); 608 for (int i = 0; i < size;) { 609 set.add(_objects[i++]); 610 } 611 } 612 print(endTime(nbrIterations * size)); 613 614 print(", contain: "); 615 startTime(); 616 for (int j = 0; j < nbrIterations; j++) { 617 for (int i = 0; i < size;) { 618 if (!set.contains(_objects[i++])) 619 throw new Error(); 620 } 621 } 622 print(endTime(nbrIterations * size)); 623 624 print(", iteration (iterator): "); 625 startTime(); 626 for (int j = 0; j < nbrIterations * 10; j++) { 627 for (Iterator i = set.iterator(); i.hasNext();) { 628 if (i.next() == set) 629 throw new Error(); 630 } 631 } 632 print(endTime(nbrIterations * size * 10)); 633 634 println(""); 635 } 636 println(""); 637 638 } 639 640 private void benchmarkLinkedHashSet() { 641 LinkedHashSet set = new LinkedHashSet(); 642 if (!set.getClass().getName().equals("java.util.LinkedHashSet")) 643 return; println(set.getClass()); 645 646 for (int size = 10; size <= MAX_COLLECTION_SIZE; size *= 10) { 647 final int nbrIterations = 100 * MAX_COLLECTION_SIZE / size; 648 print(" Size: " + size); 649 650 print(", add (new): "); 651 startTime(); 652 for (int j = 0; j < nbrIterations; j++) { 653 set = new LinkedHashSet(); 654 for (int i = 0; i < size;) { 655 set.add(_objects[i++]); 656 } 657 } 658 print(endTime(nbrIterations * size)); 659 660 print(", add (recycled): "); 661 startTime(); 662 for (int j = 0; j < nbrIterations; j++) { 663 set.clear(); 664 for (int i = 0; i < size;) { 665 set.add(_objects[i++]); 666 } 667 } 668 print(endTime(nbrIterations * size)); 669 670 print(", contain: "); 671 startTime(); 672 for (int j = 0; j < nbrIterations; j++) { 673 for (int i = 0; i < size;) { 674 if (!set.contains(_objects[i++])) 675 throw new Error(); 676 } 677 } 678 print(endTime(nbrIterations * size)); 679 680 print(", iteration (iterator): "); 681 startTime(); 682 for (int j = 0; j < nbrIterations * 10; j++) { 683 for (Iterator i = set.iterator(); i.hasNext();) { 684 if (i.next() == set) 685 throw new Error(); 686 } 687 } 688 print(endTime(nbrIterations * size * 10)); 689 690 println(""); 691 } 692 println(""); 693 694 } 695 } 696 | Popular Tags |