1 19 package bak.pcj.benchmark; 20 21 import bak.pcj.IntCollection; 22 import bak.pcj.list.IntList; 23 24 31 public class IntListBenchmark extends IntCollectionBenchmark { 32 33 private static final int SMALL_SIZE = 2000; 34 35 private IntListFactory factory; 36 37 public IntListBenchmark(IntListFactory factory) { 38 this.factory = factory; 39 } 40 41 protected IntCollection create(int[] elements) 42 { return factory.create(elements); } 43 44 48 public String benchmarkContainsExisting(DataSet dataSet) { 49 IntCollection c = create(dataSet.get(0)); 50 int[] l = dataSet.get(0); 51 startTimer(); 52 for (int i = 0; i < SMALL_SIZE; i++) 53 c.contains(l[i % l.length]); 54 stopTimer(); 55 return SMALL_SIZE + " successful calls to contains() with " + c.size() + " elements"; 56 } 57 58 public String benchmarkContainsNonExisting(DataSet dataSet) { 59 IntCollection c = create(dataSet.get(0)); 60 int[] l = dataSet.get(1); 61 startTimer(); 62 for (int i = 0; i < SMALL_SIZE; i++) 63 c.contains(l[i % l.length]); 64 stopTimer(); 65 return SMALL_SIZE + " unsuccessful calls to contains() with " + c.size() + " elements"; 66 } 67 68 public String benchmarkRemoveExisting(DataSet dataSet) { 69 IntCollection c = create(dataSet.get(0)); 70 int[] l = dataSet.get(0); 71 startTimer(); 72 for (int i = 0; i < SMALL_SIZE; i++) 73 c.remove(l[i % l.length]); 74 stopTimer(); 75 return SMALL_SIZE + " successful calls to remove() with " + l.length + " existing elements"; 76 } 77 78 public String benchmarkRemoveNonExisting(DataSet dataSet) { 79 IntCollection c = create(dataSet.get(0)); 80 int[] l = dataSet.get(1); 81 startTimer(); 82 for (int i = 0; i < SMALL_SIZE; i++) 83 c.remove(l[i % l.length]); 84 stopTimer(); 85 return SMALL_SIZE + " unsuccessful calls to remove() with " + l.length + " existing elements"; 86 } 87 88 92 public String benchmarkAddMiddle(DataSet dataSet) { 93 IntList c = (IntList)create(dataSet.get(0)); 94 int[] l = dataSet.get(0); 95 int size = l.length; 96 startTimer(); 97 for (int i = 0; i < SMALL_SIZE; i++) { 98 c.add(size/2, l[i % l.length]); 99 size++; 100 } 101 stopTimer(); 102 return SMALL_SIZE + " calls to add(int,int) at middle of list with " + l.length + " existing elements"; 103 } 104 105 public String benchmarkAddBeginning(DataSet dataSet) { 106 IntList c = (IntList)create(dataSet.get(0)); 107 int[] l = dataSet.get(0); 108 int size = l.length; 109 startTimer(); 110 for (int i = 0; i < SMALL_SIZE; i++) { 111 c.add(0, l[i % l.length]); 112 size++; 113 } 114 stopTimer(); 115 return SMALL_SIZE + " calls to add(int,int) at beginning of list with " + l.length + " existing elements"; 116 } 117 118 public String benchmarkRemoveMiddle(DataSet dataSet) { 119 IntList c = (IntList)create(dataSet.get(0)); 120 int[] l = dataSet.get(0); 121 int size = l.length; 122 startTimer(); 123 for (int i = 0; i < SMALL_SIZE; i++) { 124 if (size == 0) break; 125 c.removeElementAt(size/2); 126 size--; 127 } 128 stopTimer(); 129 return SMALL_SIZE + " calls to removeElementAt(int) at middle of list with " + l.length + " existing elements"; 130 } 131 132 public String benchmarkRemoveBeginning(DataSet dataSet) { 133 IntList c = (IntList)create(dataSet.get(0)); 134 int[] l = dataSet.get(0); 135 int size = l.length; 136 startTimer(); 137 for (int i = 0; i < SMALL_SIZE; i++) { 138 if (size == 0) break; 139 c.removeElementAt(0); 140 size--; 141 } 142 stopTimer(); 143 return SMALL_SIZE + " calls to removeElementAt(int) at beginning of list with " + l.length + " existing elements"; 144 } 145 146 public String benchmarkRemoveEnd(DataSet dataSet) { 147 IntList c = (IntList)create(dataSet.get(0)); 148 int[] l = dataSet.get(0); 149 int size = l.length; 150 startTimer(); 151 for (int i = 0; i < SMALL_SIZE; i++) { 152 if (size == 0) break; 153 c.removeElementAt(size-1); 154 size--; 155 } 156 stopTimer(); 157 return SMALL_SIZE + " calls to removeElementAt(int) at end of list with " + l.length + " existing elements"; 158 } 159 160 } | Popular Tags |