001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.benchmark;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: /**
025: * This class represents an abstract base for implementing benchmarks
026: * for collections of {@link Integer Integer} values.
027: *
028: * @author Søren Bak
029: * @version 1.0 2003/4/1
030: * @since 1.0
031: */
032: public abstract class CollectionBenchmark extends Benchmark {
033:
034: protected abstract Collection create(Integer[] elements);
035:
036: protected Collection create() {
037: return create(new Integer[] {});
038: }
039:
040: public String getClassId() {
041: Object v = create();
042: String name = v.getClass().getName();
043: return name;
044: }
045:
046: public String benchmarkAddExisting(DataSet dataSet) {
047: Collection c = create(dataSet.getObjects(0));
048: Integer[] l = dataSet.getObjects(0);
049: startTimer();
050: for (int i = 0; i < l.length; i++)
051: c.add(l[i]);
052: stopTimer();
053: return l.length + " overwriting calls to add() with "
054: + l.length + " elements";
055: }
056:
057: public String benchmarkAddNonExisting(DataSet dataSet) {
058: Collection c = create(dataSet.getObjects(0));
059: Integer[] l = dataSet.getObjects(1);
060: startTimer();
061: for (int i = 0; i < l.length; i++)
062: c.add(l[i]);
063: stopTimer();
064: return l.length + " non-overwriting calls to add() with "
065: + l.length + " elements";
066: }
067:
068: public String benchmarkContainsExisting(DataSet dataSet) {
069: Collection c = create(dataSet.getObjects(0));
070: Integer[] l = dataSet.getObjects(0);
071: startTimer();
072: for (int i = 0; i < l.length; i++)
073: c.contains(l[i]);
074: stopTimer();
075: return l.length + " successful calls to contains() with "
076: + c.size() + " elements";
077: }
078:
079: public String benchmarkContainsNonExisting(DataSet dataSet) {
080: Collection c = create(dataSet.getObjects(0));
081: Integer[] l = dataSet.getObjects(1);
082: startTimer();
083: for (int i = 0; i < l.length; i++)
084: c.contains(l[i]);
085: stopTimer();
086: return l.length + " unsuccessful calls to contains() with "
087: + c.size() + " elements";
088: }
089:
090: public String benchmarkRemoveExisting(DataSet dataSet) {
091: Collection c = create(dataSet.getObjects(0));
092: Integer[] l = dataSet.getObjects(0);
093: startTimer();
094: for (int i = 0; i < l.length; i++)
095: c.remove(l[i]);
096: stopTimer();
097: return l.length + " successful calls to remove() with "
098: + l.length + " elements";
099: }
100:
101: public String benchmarkRemoveNonExisting(DataSet dataSet) {
102: Collection c = create(dataSet.getObjects(0));
103: Integer[] l = dataSet.getObjects(1);
104: startTimer();
105: for (int i = 0; i < l.length; i++)
106: c.remove(l[i]);
107: stopTimer();
108: return l.length + " unsuccessful calls to remove() with "
109: + l.length + " elements";
110: }
111:
112: public String benchmarkIterator(DataSet dataSet) {
113: Collection c = create(dataSet.getObjects(0));
114: startTimer();
115: Iterator it = c.iterator();
116: while (it.hasNext())
117: it.next();
118: stopTimer();
119: return "Iteration over " + c.size() + " elements";
120: }
121:
122: }
|