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.Map;
022: import java.util.Iterator;
023:
024: /**
025: * This class represents an abstract base for implementing benchmarks
026: * for maps.
027: *
028: * @author Søren Bak
029: * @version 1.0 2003/5/1
030: * @since 1.0
031: */
032: public abstract class MapBenchmark extends Benchmark {
033:
034: protected abstract Map create(Integer[] keys, Integer[] values);
035:
036: protected Map create() {
037: return create(new Integer[] {}, 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 benchmarkPutExisting(DataSet dataSet) {
047: Map c = create(dataSet.getObjects(0), dataSet.getObjects(0));
048: Integer[] l = dataSet.getObjects(0);
049: Integer[] k = dataSet.getObjects(1);
050: startTimer();
051: for (int i = 0; i < l.length; i++)
052: c.put(l[i], k[i]);
053: stopTimer();
054: return l.length + " overwriting calls to put() with "
055: + l.length + " mappings";
056: }
057:
058: public String benchmarkPutNonExisting(DataSet dataSet) {
059: Map c = create(dataSet.getObjects(0), dataSet.getObjects(0));
060: Integer[] l = dataSet.getObjects(1);
061: startTimer();
062: for (int i = 0; i < l.length; i++)
063: c.put(l[i], l[i]);
064: stopTimer();
065: return l.length + " non-overwriting calls to put() with "
066: + l.length + " mappings";
067: }
068:
069: public String benchmarkGetExisting(DataSet dataSet) {
070: Map c = create(dataSet.getObjects(0), dataSet.getObjects(0));
071: Integer[] l = dataSet.getObjects(0);
072: startTimer();
073: for (int i = 0; i < l.length; i++)
074: c.get(l[i]);
075: stopTimer();
076: return l.length + " successful calls to get() with " + c.size()
077: + " mappings";
078: }
079:
080: public String benchmarkGetNonExisting(DataSet dataSet) {
081: Map c = create(dataSet.getObjects(0), dataSet.getObjects(0));
082: Integer[] l = dataSet.getObjects(1);
083: startTimer();
084: for (int i = 0; i < l.length; i++)
085: c.get(l[i]);
086: stopTimer();
087: return l.length + " unsuccessful calls to get() with "
088: + c.size() + " mappings";
089: }
090:
091: public String benchmarkRemoveExisting(DataSet dataSet) {
092: Map c = create(dataSet.getObjects(0), dataSet.getObjects(0));
093: Integer[] l = dataSet.getObjects(0);
094: startTimer();
095: for (int i = 0; i < l.length; i++)
096: c.remove(l[i]);
097: stopTimer();
098: return l.length + " successful calls to remove() with "
099: + l.length + " mappings";
100: }
101:
102: public String benchmarkRemoveNonExisting(DataSet dataSet) {
103: Map c = create(dataSet.getObjects(0), dataSet.getObjects(0));
104: Integer[] l = dataSet.getObjects(1);
105: startTimer();
106: for (int i = 0; i < l.length; i++)
107: c.remove(l[i]);
108: stopTimer();
109: return l.length + " unsuccessful calls to remove() with "
110: + l.length + " mappings";
111: }
112:
113: public String benchmarkIterator(DataSet dataSet) {
114: Map c = create(dataSet.getObjects(0), dataSet.getObjects(0));
115: startTimer();
116: Iterator it = c.entrySet().iterator();
117: while (it.hasNext()) {
118: it.next();
119: Map.Entry e = (Map.Entry) it.next();
120: e.getKey();
121: e.getValue();
122: }
123: stopTimer();
124: return "Iteration over " + c.size() + " mappings";
125: }
126:
127: }
|