1 19 package bak.pcj.benchmark; 20 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 33 public abstract class CollectionBenchmark extends Benchmark { 34 35 protected abstract Collection create(Integer [] elements); 36 37 protected Collection create() { 38 return create(new Integer []{}); 39 } 40 41 public String getClassId() { 42 Object v = create(); 43 String name = v.getClass().getName(); 44 return name; 45 } 46 47 public String benchmarkAddExisting(DataSet dataSet) { 48 Collection c = create(dataSet.getObjects(0)); 49 Integer [] l = dataSet.getObjects(0); 50 startTimer(); 51 for (int i = 0; i < l.length; i++) 52 c.add(l[i]); 53 stopTimer(); 54 return l.length + " overwriting calls to add() with " + l.length + " elements"; 55 } 56 57 public String benchmarkAddNonExisting(DataSet dataSet) { 58 Collection c = create(dataSet.getObjects(0)); 59 Integer [] l = dataSet.getObjects(1); 60 startTimer(); 61 for (int i = 0; i < l.length; i++) 62 c.add(l[i]); 63 stopTimer(); 64 return l.length + " non-overwriting calls to add() with " + l.length + " elements"; 65 } 66 67 public String benchmarkContainsExisting(DataSet dataSet) { 68 Collection c = create(dataSet.getObjects(0)); 69 Integer [] l = dataSet.getObjects(0); 70 startTimer(); 71 for (int i = 0; i < l.length; i++) 72 c.contains(l[i]); 73 stopTimer(); 74 return l.length + " successful calls to contains() with " + c.size() + " elements"; 75 } 76 77 public String benchmarkContainsNonExisting(DataSet dataSet) { 78 Collection c = create(dataSet.getObjects(0)); 79 Integer [] l = dataSet.getObjects(1); 80 startTimer(); 81 for (int i = 0; i < l.length; i++) 82 c.contains(l[i]); 83 stopTimer(); 84 return l.length + " unsuccessful calls to contains() with " + c.size() + " elements"; 85 } 86 87 public String benchmarkRemoveExisting(DataSet dataSet) { 88 Collection c = create(dataSet.getObjects(0)); 89 Integer [] l = dataSet.getObjects(0); 90 startTimer(); 91 for (int i = 0; i < l.length; i++) 92 c.remove(l[i]); 93 stopTimer(); 94 return l.length + " successful calls to remove() with " + l.length + " elements"; 95 } 96 97 public String benchmarkRemoveNonExisting(DataSet dataSet) { 98 Collection c = create(dataSet.getObjects(0)); 99 Integer [] l = dataSet.getObjects(1); 100 startTimer(); 101 for (int i = 0; i < l.length; i++) 102 c.remove(l[i]); 103 stopTimer(); 104 return l.length + " unsuccessful calls to remove() with " + l.length + " elements"; 105 } 106 107 public String benchmarkIterator(DataSet dataSet) { 108 Collection c = create(dataSet.getObjects(0)); 109 startTimer(); 110 Iterator it = c.iterator(); 111 while (it.hasNext()) it.next(); 112 stopTimer(); 113 return "Iteration over " + c.size() + " elements"; 114 } 115 116 } | Popular Tags |