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 bak.pcj.IntCollection;
022: import bak.pcj.IntIterator;
023: import bak.pcj.list.IntList;
024: import bak.pcj.list.IntArrayList;
025:
026: /**
027: * This class represents an abstract base for implementing benchmarks
028: * for collections of int values.
029: *
030: * @author Søren Bak
031: * @version 1.0 2003/5/1
032: * @since 1.0
033: */
034: public abstract class IntCollectionBenchmark extends Benchmark {
035:
036: protected abstract IntCollection create(int[] elements);
037:
038: protected IntCollection create() {
039: return create(new int[] {});
040: }
041:
042: public String getClassId() {
043: Object v = create();
044: String name = v.getClass().getName();
045: return name;
046: }
047:
048: public String benchmarkAddExisting(DataSet dataSet) {
049: IntCollection c = create(dataSet.get(0));
050: int[] l = dataSet.get(0);
051: startTimer();
052: for (int i = 0; i < l.length; i++)
053: c.add(l[i]);
054: stopTimer();
055: return l.length + " overwriting calls to add() with "
056: + l.length + " elements";
057: }
058:
059: public String benchmarkAddNonExisting(DataSet dataSet) {
060: IntCollection c = create(dataSet.get(0));
061: int[] l = dataSet.get(1);
062: startTimer();
063: for (int i = 0; i < l.length; i++)
064: c.add(l[i]);
065: stopTimer();
066: return l.length + " non-overwriting calls to add() with "
067: + l.length + " elements";
068: }
069:
070: public String benchmarkContainsExisting(DataSet dataSet) {
071: IntCollection c = create(dataSet.get(0));
072: int[] l = dataSet.get(0);
073: startTimer();
074: for (int i = 0; i < l.length; i++)
075: c.contains(l[i]);
076: stopTimer();
077: return l.length + " successful calls to contains() with "
078: + c.size() + " elements";
079: }
080:
081: public String benchmarkContainsNonExisting(DataSet dataSet) {
082: IntCollection c = create(dataSet.get(0));
083: int[] l = dataSet.get(1);
084: startTimer();
085: for (int i = 0; i < l.length; i++)
086: c.contains(l[i]);
087: stopTimer();
088: return l.length + " unsuccessful calls to contains() with "
089: + c.size() + " elements";
090: }
091:
092: public String benchmarkRemoveExisting(DataSet dataSet) {
093: IntCollection c = create(dataSet.get(0));
094: int[] l = dataSet.get(0);
095: startTimer();
096: for (int i = 0; i < l.length; i++)
097: c.remove(l[i]);
098: stopTimer();
099: return l.length + " successful calls to remove() with "
100: + l.length + " elements";
101: }
102:
103: public String benchmarkRemoveNonExisting(DataSet dataSet) {
104: IntCollection c = create(dataSet.get(0));
105: int[] l = dataSet.get(1);
106: startTimer();
107: for (int i = 0; i < l.length; i++)
108: c.remove(l[i]);
109: stopTimer();
110: return l.length + " unsuccessful calls to remove() with "
111: + l.length + " elements";
112: }
113:
114: public String benchmarkIterator(DataSet dataSet) {
115: IntCollection c = create(dataSet.get(0));
116: startTimer();
117: IntIterator it = c.iterator();
118: while (it.hasNext())
119: it.next();
120: stopTimer();
121: return "Iteration over " + c.size() + " elements";
122: }
123:
124: }
|