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.List;
023:
024: /**
025: * This class represents an abstract base for implementing benchmarks
026: * for lists of {@link Integer Integer} values.
027: *
028: * @author Søren Bak
029: * @version 1.0 2003/5/1
030: * @since 1.0
031: */
032: public abstract class ListBenchmark extends CollectionBenchmark {
033:
034: private static final int SMALL_SIZE = 2000;
035:
036: // ---------------------------------------------------------------
037: // Overriden methods
038: // ---------------------------------------------------------------
039:
040: public String benchmarkContainsExisting(DataSet dataSet) {
041: Collection c = create(dataSet.getObjects(0));
042: Integer[] l = dataSet.getObjects(0);
043: startTimer();
044: for (int i = 0; i < SMALL_SIZE; i++)
045: c.contains(l[i % l.length]);
046: stopTimer();
047: return SMALL_SIZE + " successful calls to contains() with "
048: + c.size() + " elements";
049: }
050:
051: public String benchmarkContainsNonExisting(DataSet dataSet) {
052: Collection c = create(dataSet.getObjects(0));
053: Integer[] l = dataSet.getObjects(1);
054: startTimer();
055: for (int i = 0; i < SMALL_SIZE; i++)
056: c.contains(l[i % l.length]);
057: stopTimer();
058: return SMALL_SIZE + " unsuccessful calls to contains() with "
059: + c.size() + " elements";
060: }
061:
062: public String benchmarkRemoveExisting(DataSet dataSet) {
063: Collection c = create(dataSet.getObjects(0));
064: Integer[] l = dataSet.getObjects(0);
065: startTimer();
066: for (int i = 0; i < SMALL_SIZE; i++)
067: c.remove(l[i % l.length]);
068: stopTimer();
069: return SMALL_SIZE + " successful calls to remove() with "
070: + l.length + " existing elements";
071: }
072:
073: public String benchmarkRemoveNonExisting(DataSet dataSet) {
074: Collection c = create(dataSet.getObjects(0));
075: Integer[] l = dataSet.getObjects(1);
076: startTimer();
077: for (int i = 0; i < SMALL_SIZE; i++)
078: c.remove(l[i % l.length]);
079: stopTimer();
080: return SMALL_SIZE + " unsuccessful calls to remove() with "
081: + l.length + " existing elements";
082: }
083:
084: // ---------------------------------------------------------------
085: // List methods
086: // ---------------------------------------------------------------
087:
088: public String benchmarkAddMiddle(DataSet dataSet) {
089: List c = (List) create(dataSet.getObjects(0));
090: Integer[] l = dataSet.getObjects(0);
091: int size = l.length;
092: startTimer();
093: for (int i = 0; i < SMALL_SIZE; i++) {
094: c.add(size / 2, l[i % l.length]);
095: size++;
096: }
097: stopTimer();
098: return SMALL_SIZE
099: + " calls to add(int,int) at middle of list with "
100: + l.length + " existing elements";
101: }
102:
103: public String benchmarkAddBeginning(DataSet dataSet) {
104: List c = (List) create(dataSet.getObjects(0));
105: Integer[] l = dataSet.getObjects(0);
106: int size = l.length;
107: startTimer();
108: for (int i = 0; i < SMALL_SIZE; i++) {
109: c.add(0, l[i % l.length]);
110: size++;
111: }
112: stopTimer();
113: return SMALL_SIZE
114: + " calls to add(int,int) at beginning of list with "
115: + l.length + " existing elements";
116: }
117:
118: public String benchmarkRemoveMiddle(DataSet dataSet) {
119: List c = (List) create(dataSet.getObjects(0));
120: Integer[] l = dataSet.getObjects(0);
121: int size = l.length;
122: startTimer();
123: for (int i = 0; i < SMALL_SIZE; i++) {
124: if (size == 0)
125: break;
126: c.remove(size / 2);
127: size--;
128: }
129: stopTimer();
130: return SMALL_SIZE
131: + " calls to remove(int) at middle of list with "
132: + l.length + " existing elements";
133: }
134:
135: public String benchmarkRemoveBeginning(DataSet dataSet) {
136: List c = (List) create(dataSet.getObjects(0));
137: Integer[] l = dataSet.getObjects(0);
138: int size = l.length;
139: startTimer();
140: for (int i = 0; i < SMALL_SIZE; i++) {
141: if (size == 0)
142: break;
143: c.remove(0);
144: size--;
145: }
146: stopTimer();
147: return SMALL_SIZE
148: + " calls to removeElementAt(int) at beginning of list with "
149: + l.length + " existing elements";
150: }
151:
152: public String benchmarkRemoveEnd(DataSet dataSet) {
153: List c = (List) create(dataSet.getObjects(0));
154: Integer[] l = dataSet.getObjects(0);
155: int size = l.length;
156: startTimer();
157: for (int i = 0; i < SMALL_SIZE; i++) {
158: if (size == 0)
159: break;
160: c.remove(size - 1);
161: size--;
162: }
163: stopTimer();
164: return SMALL_SIZE
165: + " calls to removeElementAt(int) at end of list with "
166: + l.length + " existing elements";
167: }
168:
169: }
|