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