001: /*=============================================================================
002: * Copyright Texas Instruments 2003. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.util;
022:
023: import java.util.Hashtable;
024:
025: /**
026: * A benchmark to compare OpenHashSymbolTable to a java.util.Hashtable for
027: * operations that are likely to be commonly used by the script engine
028: *
029: * @author Rob Clark
030: * @version 0.1
031: */
032: public class TableBench {
033: private static int[] TABLE_SIZES = { 1, 3, 5, 10, 30, 50 };
034: private static int INITIAL_SIZE = 3;
035: private static float LOAD = 0.67f;
036: private static int COUNT = 1000000;
037: private static Object VALUE = Boolean.TRUE; // what the key maps to
038:
039: public static void main(String[] args) throws Throwable {
040: for (int i = 0; i < TABLE_SIZES.length; i++) {
041: long t;
042: System.err.println("Results for table size: "
043: + TABLE_SIZES[i]);
044:
045: System.gc();
046: Thread.sleep(200);
047:
048: //////////////////////////////////////////////////////////////////////////
049: System.err.println("** TEST 1: build and populate table");
050:
051: t = System.currentTimeMillis();
052: for (int j = 0; j < COUNT; j++) {
053: Hashtable tbl = new Hashtable(INITIAL_SIZE, LOAD);
054: for (int k = 0; k <= TABLE_SIZES[i]; k++)
055: tbl.put(new Integer(p(k)), VALUE);
056: }
057: System.err.println(" java.util.Hashtable: "
058: + (System.currentTimeMillis() - t) + "ms");
059: System.gc();
060: Thread.sleep(200);
061:
062: t = System.currentTimeMillis();
063: for (int j = 0; j < COUNT; j++) {
064: SymbolMap tbl = new SymbolMap(new OpenHashSymbolTable(
065: INITIAL_SIZE, LOAD));
066: for (int k = 0; k <= TABLE_SIZES[i]; k++)
067: tbl.put(p(k), VALUE);
068: }
069: System.err.println(" SymbolMap(OpenHashSymbolTable): "
070: + (System.currentTimeMillis() - t) + "ms");
071: System.gc();
072: Thread.sleep(200);
073:
074: t = System.currentTimeMillis();
075: for (int j = 0; j < COUNT; j++) {
076: OpenHashSymbolTable tbl = new OpenHashSymbolTable(
077: INITIAL_SIZE, LOAD);
078: for (int k = 0; k <= TABLE_SIZES[i]; k++)
079: tbl.create(p(k));
080: }
081: System.err.println(" OpenHashSymbolTable: "
082: + (System.currentTimeMillis() - t) + "ms");
083: System.gc();
084: Thread.sleep(200);
085:
086: //////////////////////////////////////////////////////////////////////////
087: System.err.println("** TEST 2: access member");
088:
089: {
090: // first we need to populate the table:
091: Hashtable tbl = new Hashtable(INITIAL_SIZE, LOAD);
092: for (int k = 0; k < TABLE_SIZES[i]; k++)
093: tbl.put(new Integer(p(k)), VALUE);
094:
095: // and then for the actual test:
096: t = System.currentTimeMillis();
097: for (int j = 0; j < COUNT; j++)
098: tbl.get(new Integer(p(j % TABLE_SIZES[i])));
099: System.err
100: .println(" java.util.Hashtable: "
101: + (System.currentTimeMillis() - t)
102: + "ms");
103: }
104: System.gc();
105: Thread.sleep(200);
106:
107: {
108: // first we need to populate the table:
109: SymbolMap tbl = new SymbolMap(new OpenHashSymbolTable(
110: INITIAL_SIZE, LOAD));
111: for (int k = 0; k < TABLE_SIZES[i]; k++)
112: tbl.put(p(k), VALUE);
113:
114: // and then for the actual test:
115: t = System.currentTimeMillis();
116: for (int j = 0; j < COUNT; j++)
117: tbl.get(p(j % TABLE_SIZES[i]));
118: System.err
119: .println(" SymbolMap(OpenHashSymbolTable): "
120: + (System.currentTimeMillis() - t)
121: + "ms");
122: }
123: System.gc();
124: Thread.sleep(200);
125:
126: {
127: // first we need to populate the table:
128: OpenHashSymbolTable tbl = new OpenHashSymbolTable(
129: INITIAL_SIZE, LOAD);
130: for (int k = 0; k < TABLE_SIZES[i]; k++)
131: tbl.create(p(k));
132:
133: // and then for the actual test:
134: t = System.currentTimeMillis();
135: for (int j = 0; j < COUNT; j++)
136: tbl.get(p(j % TABLE_SIZES[i]));
137: System.err
138: .println(" OpenHashSymbolTable: "
139: + (System.currentTimeMillis() - t)
140: + "ms");
141: }
142: System.gc();
143: Thread.sleep(200);
144:
145: //////////////////////////////////////////////////////////////////////////
146: System.err.println("** TEST 3: access non-member");
147:
148: {
149: // first we need to populate the table:
150: Hashtable tbl = new Hashtable(INITIAL_SIZE, LOAD);
151: for (int k = 0; k < TABLE_SIZES[i]; k++)
152: tbl.put(new Integer(p(k)), VALUE);
153:
154: // and then for the actual test:
155: t = System.currentTimeMillis();
156: for (int j = 0; j < COUNT; j++)
157: tbl.get(new Integer((j % TABLE_SIZES[i])
158: + TABLE_SIZES[i] + 1));
159: System.err
160: .println(" java.util.Hashtable: "
161: + (System.currentTimeMillis() - t)
162: + "ms");
163: }
164: System.gc();
165: Thread.sleep(200);
166:
167: {
168: // first we need to populate the table:
169: SymbolMap tbl = new SymbolMap(new OpenHashSymbolTable(
170: INITIAL_SIZE, LOAD));
171: for (int k = 0; k < TABLE_SIZES[i]; k++)
172: tbl.put(p(k), VALUE);
173:
174: // and then for the actual test:
175: t = System.currentTimeMillis();
176: for (int j = 0; j < COUNT; j++)
177: tbl.get((j % TABLE_SIZES[i]) + TABLE_SIZES[i] + 1);
178: System.err
179: .println(" SymbolMap(OpenHashSymbolTable): "
180: + (System.currentTimeMillis() - t)
181: + "ms");
182: }
183: System.gc();
184: Thread.sleep(200);
185:
186: {
187: // first we need to populate the table:
188: OpenHashSymbolTable tbl = new OpenHashSymbolTable(
189: INITIAL_SIZE, LOAD);
190: for (int k = 0; k < TABLE_SIZES[i]; k++)
191: tbl.create(p(k));
192:
193: // and then for the actual test:
194: t = System.currentTimeMillis();
195: for (int j = 0; j < COUNT; j++)
196: tbl.get((j % TABLE_SIZES[i]) + TABLE_SIZES[i] + 1);
197: System.err
198: .println(" OpenHashSymbolTable: "
199: + (System.currentTimeMillis() - t)
200: + "ms");
201: }
202: System.gc();
203: Thread.sleep(200);
204: }
205: }
206:
207: private static final int p(int p) {
208: return Primes.PRIMES[p % Primes.PRIMES.length];
209: }
210: }
211:
212: /*
213: * Local Variables:
214: * tab-width: 2
215: * indent-tabs-mode: nil
216: * mode: java
217: * c-indentation-style: java
218: * c-basic-offset: 2
219: * eval: (c-set-offset 'substatement-open '0)
220: * eval: (c-set-offset 'case-label '+)
221: * eval: (c-set-offset 'inclass '+)
222: * eval: (c-set-offset 'inline-open '0)
223: * End:
224: */
|