KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntIterator;
23 import bak.pcj.list.IntList;
24 import bak.pcj.list.IntArrayList;
25
26 /**
27  * This class represents an abstract base for implementing benchmarks
28  * for collections of int values.
29  *
30  * @author Søren Bak
31  * @version 1.0 2003/5/1
32  * @since 1.0
33  */

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