KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * This class represents an abstract base for implementing benchmarks
27  * for lists of {@link Integer Integer} values.
28  *
29  * @author Søren Bak
30  * @version 1.0 2003/5/1
31  * @since 1.0
32  */

33 public abstract class ListBenchmark extends CollectionBenchmark {
34
35     private static final int SMALL_SIZE = 2000;
36
37     // ---------------------------------------------------------------
38
// Overriden methods
39
// ---------------------------------------------------------------
40

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

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