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