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