KickJava   Java API By Example, From Geeks To Geeks.

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


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.map.IntKeyMap;
22 import bak.pcj.map.IntKeyMapIterator;
23
24 /**
25  * This class represents an abstract base for implementing benchmarks
26  * for maps of int keys.
27  *
28  * @author Søren Bak
29  * @version 1.0 2003/8/1
30  * @since 1.0
31  */

32 public abstract class IntKeyMapBenchmark extends Benchmark {
33
34     private IntKeyMapFactory factory;
35
36     public IntKeyMapBenchmark(IntKeyMapFactory factory) {
37         this.factory = factory;
38     }
39
40     protected IntKeyMap create(int[] keys, Integer JavaDoc[] values) {
41         return factory.create(keys, values);
42     }
43
44     protected IntKeyMap create() {
45         return create(new int[]{}, new Integer JavaDoc[]{});
46     }
47
48     public String JavaDoc getClassId() {
49         Object JavaDoc v = create();
50         String JavaDoc name = v.getClass().getName();
51         return name;
52     }
53
54     public String JavaDoc benchmarkPutExisting(DataSet dataSet) {
55         IntKeyMap c = create(dataSet.get(0), dataSet.getObjects(0));
56         int[] l = dataSet.get(0);
57         Integer JavaDoc[] k = dataSet.getObjects(1);
58         startTimer();
59         for (int i = 0; i < l.length; i++)
60             c.put(l[i], k[i]);
61         stopTimer();
62         return l.length + " overwriting calls to put() with " + l.length + " mappings";
63     }
64
65     public String JavaDoc benchmarkPutNonExisting(DataSet dataSet) {
66         IntKeyMap c = create(dataSet.get(0), dataSet.getObjects(0));
67         int[] l = dataSet.get(1);
68         Integer JavaDoc[] ll = dataSet.getObjects(1);
69         startTimer();
70         for (int i = 0; i < l.length; i++)
71             c.put(l[i], ll[i]);
72         stopTimer();
73         return l.length + " non-overwriting calls to put() with " + l.length + " mappings";
74     }
75
76     public String JavaDoc benchmarkGetExisting(DataSet dataSet) {
77         IntKeyMap c = create(dataSet.get(0), dataSet.getObjects(0));
78         int[] l = dataSet.get(0);
79         startTimer();
80         for (int i = 0; i < l.length; i++)
81             c.get(l[i]);
82         stopTimer();
83         return l.length + " successful calls to get() with " + c.size() + " mappings";
84     }
85
86     public String JavaDoc benchmarkGetNonExisting(DataSet dataSet) {
87         IntKeyMap c = create(dataSet.get(0), dataSet.getObjects(0));
88         int[] l = dataSet.get(1);
89         startTimer();
90         for (int i = 0; i < l.length; i++)
91             c.get(l[i]);
92         stopTimer();
93         return l.length + " unsuccessful calls to get() with " + c.size() + " mappings";
94     }
95
96     public String JavaDoc benchmarkRemoveExisting(DataSet dataSet) {
97         IntKeyMap c = create(dataSet.get(0), dataSet.getObjects(0));
98         int[] l = dataSet.get(0);
99         startTimer();
100         for (int i = 0; i < l.length; i++)
101             c.remove(l[i]);
102         stopTimer();
103         return l.length + " successful calls to remove() with " + l.length + " mappings";
104     }
105
106     public String JavaDoc benchmarkRemoveNonExisting(DataSet dataSet) {
107         IntKeyMap c = create(dataSet.get(0), dataSet.getObjects(0));
108         int[] l = dataSet.get(1);
109         startTimer();
110         for (int i = 0; i < l.length; i++)
111             c.remove(l[i]);
112         stopTimer();
113         return l.length + " unsuccessful calls to remove() with " + l.length + " mappings";
114     }
115
116     public String JavaDoc benchmarkIterator(DataSet dataSet) {
117         IntKeyMap c = create(dataSet.get(0), dataSet.getObjects(0));
118         startTimer();
119         IntKeyMapIterator it = c.entries();
120         while (it.hasNext()) {
121             it.next();
122             it.getKey();
123             it.getValue();
124         }
125         stopTimer();
126         return "Iteration over " + c.size() + " mappings";
127     }
128
129 }
Popular Tags