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