KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > benchmark > IntListBenchmark


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.benchmark;
20
21 import bak.pcj.IntCollection;
22 import bak.pcj.list.IntList;
23
24 /**
25  * This class represents benchmark tests for lists of int values.
26  *
27  * @author Søren Bak
28  * @version 1.0 2003/5/1
29  * @since 1.0
30  */

31 public class IntListBenchmark extends IntCollectionBenchmark {
32
33     private static final int SMALL_SIZE = 2000;
34
35     private IntListFactory factory;
36
37     public IntListBenchmark(IntListFactory factory) {
38         this.factory = factory;
39     }
40
41     protected IntCollection create(int[] elements)
42     { return factory.create(elements); }
43
44     // ---------------------------------------------------------------
45
// Overriden methods
46
// ---------------------------------------------------------------
47

48     public String JavaDoc benchmarkContainsExisting(DataSet dataSet) {
49         IntCollection c = create(dataSet.get(0));
50         int[] l = dataSet.get(0);
51         startTimer();
52         for (int i = 0; i < SMALL_SIZE; i++)
53             c.contains(l[i % l.length]);
54         stopTimer();
55         return SMALL_SIZE + " successful calls to contains() with " + c.size() + " elements";
56     }
57
58     public String JavaDoc benchmarkContainsNonExisting(DataSet dataSet) {
59         IntCollection c = create(dataSet.get(0));
60         int[] l = dataSet.get(1);
61         startTimer();
62         for (int i = 0; i < SMALL_SIZE; i++)
63             c.contains(l[i % l.length]);
64         stopTimer();
65         return SMALL_SIZE + " unsuccessful calls to contains() with " + c.size() + " elements";
66     }
67
68     public String JavaDoc benchmarkRemoveExisting(DataSet dataSet) {
69         IntCollection c = create(dataSet.get(0));
70         int[] l = dataSet.get(0);
71         startTimer();
72         for (int i = 0; i < SMALL_SIZE; i++)
73             c.remove(l[i % l.length]);
74         stopTimer();
75         return SMALL_SIZE + " successful calls to remove() with " + l.length + " existing elements";
76     }
77
78     public String JavaDoc benchmarkRemoveNonExisting(DataSet dataSet) {
79         IntCollection c = create(dataSet.get(0));
80         int[] l = dataSet.get(1);
81         startTimer();
82         for (int i = 0; i < SMALL_SIZE; i++)
83             c.remove(l[i % l.length]);
84         stopTimer();
85         return SMALL_SIZE + " unsuccessful calls to remove() with " + l.length + " existing elements";
86     }
87
88     // ---------------------------------------------------------------
89
// List methods
90
// ---------------------------------------------------------------
91

92     public String JavaDoc benchmarkAddMiddle(DataSet dataSet) {
93         IntList c = (IntList)create(dataSet.get(0));
94         int[] l = dataSet.get(0);
95         int size = l.length;
96         startTimer();
97         for (int i = 0; i < SMALL_SIZE; i++) {
98             c.add(size/2, l[i % l.length]);
99             size++;
100         }
101         stopTimer();
102         return SMALL_SIZE + " calls to add(int,int) at middle of list with " + l.length + " existing elements";
103     }
104
105     public String JavaDoc benchmarkAddBeginning(DataSet dataSet) {
106         IntList c = (IntList)create(dataSet.get(0));
107         int[] l = dataSet.get(0);
108         int size = l.length;
109         startTimer();
110         for (int i = 0; i < SMALL_SIZE; i++) {
111             c.add(0, l[i % l.length]);
112             size++;
113         }
114         stopTimer();
115         return SMALL_SIZE + " calls to add(int,int) at beginning of list with " + l.length + " existing elements";
116     }
117
118     public String JavaDoc benchmarkRemoveMiddle(DataSet dataSet) {
119         IntList c = (IntList)create(dataSet.get(0));
120         int[] l = dataSet.get(0);
121         int size = l.length;
122         startTimer();
123         for (int i = 0; i < SMALL_SIZE; i++) {
124             if (size == 0) break;
125             c.removeElementAt(size/2);
126             size--;
127         }
128         stopTimer();
129         return SMALL_SIZE + " calls to removeElementAt(int) at middle of list with " + l.length + " existing elements";
130     }
131
132     public String JavaDoc benchmarkRemoveBeginning(DataSet dataSet) {
133         IntList c = (IntList)create(dataSet.get(0));
134         int[] l = dataSet.get(0);
135         int size = l.length;
136         startTimer();
137         for (int i = 0; i < SMALL_SIZE; i++) {
138             if (size == 0) break;
139             c.removeElementAt(0);
140             size--;
141         }
142         stopTimer();
143         return SMALL_SIZE + " calls to removeElementAt(int) at beginning of list with " + l.length + " existing elements";
144     }
145
146     public String JavaDoc benchmarkRemoveEnd(DataSet dataSet) {
147         IntList c = (IntList)create(dataSet.get(0));
148         int[] l = dataSet.get(0);
149         int size = l.length;
150         startTimer();
151         for (int i = 0; i < SMALL_SIZE; i++) {
152             if (size == 0) break;
153             c.removeElementAt(size-1);
154             size--;
155         }
156         stopTimer();
157         return SMALL_SIZE + " calls to removeElementAt(int) at end of list with " + l.length + " existing elements";
158     }
159
160 }
Popular Tags