KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public abstract class IntKeyIntMapBenchmark extends Benchmark {
33
34     private IntKeyIntMapFactory factory;
35
36     public IntKeyIntMapBenchmark(IntKeyIntMapFactory factory) {
37         this.factory = factory;
38     }
39
40     protected IntKeyIntMap create(int[] keys, int[] values) {
41         return factory.create(keys, values);
42     }
43
44     protected IntKeyIntMap create() {
45         return create(new int[]{}, new int[]{});
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         IntKeyIntMap c = create(dataSet.get(0), dataSet.get(0));
56         int[] l = dataSet.get(0);
57         int[] k = dataSet.get(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         IntKeyIntMap c = create(dataSet.get(0), dataSet.get(0));
67         int[] l = dataSet.get(1);
68         startTimer();
69         for (int i = 0; i < l.length; i++)
70             c.put(l[i], l[i]);
71         stopTimer();
72         return l.length + " non-overwriting calls to put() with " + l.length + " mappings";
73     }
74
75     public String JavaDoc benchmarkGetExisting(DataSet dataSet) {
76         IntKeyIntMap c = create(dataSet.get(0), dataSet.get(0));
77         int[] l = dataSet.get(0);
78         startTimer();
79         for (int i = 0; i < l.length; i++)
80             c.get(l[i]);
81         stopTimer();
82         return l.length + " successful calls to get() with " + c.size() + " mappings";
83     }
84
85     public String JavaDoc benchmarkGetNonExisting(DataSet dataSet) {
86         IntKeyIntMap c = create(dataSet.get(0), dataSet.get(0));
87         int[] l = dataSet.get(1);
88         startTimer();
89         for (int i = 0; i < l.length; i++)
90             c.get(l[i]);
91         stopTimer();
92         return l.length + " unsuccessful calls to get() with " + c.size() + " mappings";
93     }
94
95     public String JavaDoc benchmarkRemoveExisting(DataSet dataSet) {
96         IntKeyIntMap c = create(dataSet.get(0), dataSet.get(0));
97         int[] l = dataSet.get(0);
98         startTimer();
99         for (int i = 0; i < l.length; i++)
100             c.remove(l[i]);
101         stopTimer();
102         return l.length + " successful calls to remove() with " + l.length + " mappings";
103     }
104
105     public String JavaDoc benchmarkRemoveNonExisting(DataSet dataSet) {
106         IntKeyIntMap c = create(dataSet.get(0), dataSet.get(0));
107         int[] l = dataSet.get(1);
108         startTimer();
109         for (int i = 0; i < l.length; i++)
110             c.remove(l[i]);
111         stopTimer();
112         return l.length + " unsuccessful calls to remove() with " + l.length + " mappings";
113     }
114
115     public String JavaDoc benchmarkIterator(DataSet dataSet) {
116         IntKeyIntMap c = create(dataSet.get(0), dataSet.get(0));
117         startTimer();
118         IntKeyIntMapIterator it = c.entries();
119         while (it.hasNext()) {
120             it.next();
121             it.getKey();
122             it.getValue();
123         }
124         stopTimer();
125         return "Iteration over " + c.size() + " mappings";
126     }
127
128 }
Popular Tags