KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 public abstract class CollectionBenchmark extends Benchmark {
34
35     protected abstract Collection JavaDoc create(Integer JavaDoc[] elements);
36
37     protected Collection JavaDoc create() {
38         return create(new Integer JavaDoc[]{});
39     }
40
41     public String JavaDoc getClassId() {
42         Object JavaDoc v = create();
43         String JavaDoc name = v.getClass().getName();
44         return name;
45     }
46
47     public String JavaDoc benchmarkAddExisting(DataSet dataSet) {
48         Collection JavaDoc c = create(dataSet.getObjects(0));
49         Integer JavaDoc[] l = dataSet.getObjects(0);
50         startTimer();
51         for (int i = 0; i < l.length; i++)
52             c.add(l[i]);
53         stopTimer();
54         return l.length + " overwriting calls to add() with " + l.length + " elements";
55     }
56
57     public String JavaDoc benchmarkAddNonExisting(DataSet dataSet) {
58         Collection JavaDoc c = create(dataSet.getObjects(0));
59         Integer JavaDoc[] l = dataSet.getObjects(1);
60         startTimer();
61         for (int i = 0; i < l.length; i++)
62             c.add(l[i]);
63         stopTimer();
64         return l.length + " non-overwriting calls to add() with " + l.length + " elements";
65     }
66
67     public String JavaDoc benchmarkContainsExisting(DataSet dataSet) {
68         Collection JavaDoc c = create(dataSet.getObjects(0));
69         Integer JavaDoc[] l = dataSet.getObjects(0);
70         startTimer();
71         for (int i = 0; i < l.length; i++)
72             c.contains(l[i]);
73         stopTimer();
74         return l.length + " successful calls to contains() with " + c.size() + " elements";
75     }
76
77     public String JavaDoc benchmarkContainsNonExisting(DataSet dataSet) {
78         Collection JavaDoc c = create(dataSet.getObjects(0));
79         Integer JavaDoc[] l = dataSet.getObjects(1);
80         startTimer();
81         for (int i = 0; i < l.length; i++)
82             c.contains(l[i]);
83         stopTimer();
84         return l.length + " unsuccessful calls to contains() with " + c.size() + " elements";
85     }
86
87     public String JavaDoc benchmarkRemoveExisting(DataSet dataSet) {
88         Collection JavaDoc c = create(dataSet.getObjects(0));
89         Integer JavaDoc[] l = dataSet.getObjects(0);
90         startTimer();
91         for (int i = 0; i < l.length; i++)
92             c.remove(l[i]);
93         stopTimer();
94         return l.length + " successful calls to remove() with " + l.length + " elements";
95     }
96
97     public String JavaDoc benchmarkRemoveNonExisting(DataSet dataSet) {
98         Collection JavaDoc c = create(dataSet.getObjects(0));
99         Integer JavaDoc[] l = dataSet.getObjects(1);
100         startTimer();
101         for (int i = 0; i < l.length; i++)
102             c.remove(l[i]);
103         stopTimer();
104         return l.length + " unsuccessful calls to remove() with " + l.length + " elements";
105     }
106
107     public String JavaDoc benchmarkIterator(DataSet dataSet) {
108         Collection JavaDoc c = create(dataSet.getObjects(0));
109         startTimer();
110         Iterator JavaDoc it = c.iterator();
111         while (it.hasNext()) it.next();
112         stopTimer();
113         return "Iteration over " + c.size() + " elements";
114     }
115
116 }
Popular Tags