001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.test;
032:
033: import org.hsqldb.lib.DoubleIntIndex;
034: import org.hsqldb.lib.HashSet;
035: import org.hsqldb.lib.IntKeyHashMap;
036: import org.hsqldb.lib.IntKeyIntValueHashMap;
037: import org.hsqldb.lib.IntValueHashMap;
038: import org.hsqldb.lib.StopWatch;
039:
040: /**
041: * @author fredt@users
042: */
043: public class TestLibSpeed {
044:
045: static final String[][] sNumeric = {
046: { "ABS", "org.hsqldb.Library.abs" },
047: { "ACOS", "java.lang.Math.acos" },
048: { "ASIN", "java.lang.Math.asin" },
049: { "ATAN", "java.lang.Math.atan" },
050: { "ATAN2", "java.lang.Math.atan2" },
051: { "CEILING", "java.lang.Math.ceil" },
052: { "COS", "java.lang.Math.cos" },
053: { "COT", "org.hsqldb.Library.cot" },
054: { "DEGREES", "java.lang.Math.toDegrees" },
055: { "EXP", "java.lang.Math.exp" },
056: { "FLOOR", "java.lang.Math.floor" },
057: { "LOG", "java.lang.Math.log" },
058: { "LOG10", "org.hsqldb.Library.log10" },
059: { "MOD", "org.hsqldb.Library.mod" },
060: { "PI", "org.hsqldb.Library.pi" },
061: { "POWER", "java.lang.Math.pow" },
062: { "RADIANS", "java.lang.Math.toRadians" },
063: { "RAND", "java.lang.Math.random" },
064: { "ROUND", "org.hsqldb.Library.round" },
065: { "SIGN", "org.hsqldb.Library.sign" },
066: { "SIN", "java.lang.Math.sin" },
067: { "SQRT", "java.lang.Math.sqrt" },
068: { "TAN", "java.lang.Math.tan" },
069: { "TRUNCATE", "org.hsqldb.Library.truncate" },
070: { "BITAND", "org.hsqldb.Library.bitand" },
071: { "BITOR", "org.hsqldb.Library.bitor" },
072: { "ROUNDMAGIC", "org.hsqldb.Library.roundMagic" } };
073: static HashSet hashSet = new HashSet();
074: static DoubleIntIndex doubleIntLookup = new DoubleIntIndex(
075: sNumeric.length, false);
076: static IntKeyIntValueHashMap intKeyIntValueHashLookup = new IntKeyIntValueHashMap();
077: static IntValueHashMap intValueHashLookup = new IntValueHashMap(
078: sNumeric.length);
079: static IntKeyHashMap intKeyHashLookup = new IntKeyHashMap();
080:
081: static {
082: doubleIntLookup.setKeysSearchTarget();
083:
084: java.util.Random randomgen = new java.util.Random();
085: int[] row = new int[2];
086:
087: for (int i = 0; i < sNumeric.length; i++) {
088: hashSet.add(sNumeric[i][0]);
089: intKeyIntValueHashLookup.put(randomgen
090: .nextInt(sNumeric.length), i);
091: intKeyHashLookup.put(i, new Integer(i));
092: doubleIntLookup.add(randomgen.nextInt(sNumeric.length), i);
093: intValueHashLookup.put(sNumeric[i][0], randomgen
094: .nextInt(sNumeric.length));
095: }
096: }
097:
098: static int count = 100000;
099:
100: public TestLibSpeed() {
101:
102: java.util.Random randomgen = new java.util.Random();
103: StopWatch sw = new StopWatch();
104: int dummy = 0;
105:
106: System.out.println("set lookup ");
107:
108: for (int k = 0; k < 3; k++) {
109: sw.zero();
110:
111: for (int j = 0; j < count; j++) {
112: for (int i = 0; i < sNumeric.length; i++) {
113: int r = randomgen.nextInt(sNumeric.length);
114:
115: hashSet.contains(sNumeric[r][0]);
116:
117: dummy += r;
118: }
119: }
120:
121: System.out.println("HashSet contains " + sw.elapsedTime());
122: sw.zero();
123:
124: for (int j = 0; j < count; j++) {
125: for (int i = 0; i < sNumeric.length; i++) {
126: int r = randomgen.nextInt(sNumeric.length);
127:
128: intKeyIntValueHashLookup.get(r, -1);
129:
130: dummy += r;
131: }
132: }
133:
134: System.out
135: .println("IntKeyIntValueHashMap Lookup with array "
136: + sw.elapsedTime());
137: sw.zero();
138:
139: for (int j = 0; j < count; j++) {
140: for (int i = 0; i < sNumeric.length; i++) {
141: int r = randomgen.nextInt(sNumeric.length);
142:
143: intKeyHashLookup.get(r);
144:
145: dummy += r;
146: }
147: }
148:
149: System.out.println("IntKeyHashMap Lookup "
150: + sw.elapsedTime());
151: sw.zero();
152:
153: for (int j = 0; j < count; j++) {
154: for (int i = 0; i < sNumeric.length; i++) {
155: int r = randomgen.nextInt(sNumeric.length);
156:
157: doubleIntLookup.findFirstEqualKeyIndex(r);
158:
159: dummy += r;
160: }
161: }
162:
163: System.out.println("DoubleIntTable Lookup "
164: + sw.elapsedTime());
165: sw.zero();
166:
167: for (int j = 0; j < count; j++) {
168: for (int i = 0; i < sNumeric.length; i++) {
169: int r = randomgen.nextInt(sNumeric.length);
170:
171: intValueHashLookup.get(sNumeric[r][0], 0);
172:
173: dummy += r;
174: }
175: }
176:
177: System.out.println("IntKeyIntValueHashMap Lookup "
178: + sw.elapsedTime());
179: sw.zero();
180:
181: for (int j = 0; j < count; j++) {
182: for (int i = 0; i < sNumeric.length; i++) {
183: int r = randomgen.nextInt(sNumeric.length);
184:
185: dummy += r;
186: }
187: }
188:
189: System.out.println("emptyOp " + sw.elapsedTime());
190: sw.zero();
191:
192: for (int j = 0; j < count; j++) {
193: for (int i = 0; i < sNumeric.length; i++) {
194: int r = randomgen.nextInt(sNumeric.length);
195:
196: doubleIntLookup.findFirstEqualKeyIndex(r);
197:
198: dummy += r;
199: }
200: }
201:
202: System.out.println("DoubleIntTable Lookup "
203: + sw.elapsedTime());
204: sw.zero();
205: System.out.println("Object Cache Test " + sw.elapsedTime());
206: }
207: }
208:
209: public static void main(String[] argv) {
210: TestLibSpeed ls = new TestLibSpeed();
211: }
212: }
|