001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.util;
017:
018: import java.util.Random;
019: import java.util.BitSet;
020:
021: /** Performance tester for OpenBitSet.
022: * Use -Xbatch for more predictable results, and run tests such that the duration
023: * is at least 10 seconds for better accuracy. Close browsers on your system (javascript
024: * or flash may be running and cause more erratic results).
025: *
026: * @author yonik
027: * @version $Id$
028: */
029: public class BitSetPerf {
030: static Random rand = new Random(0);
031:
032: static void randomSets(int maxSize, int bitsToSet, BitSet target1,
033: OpenBitSet target2) {
034: for (int i = 0; i < bitsToSet; i++) {
035: int idx;
036: do {
037: idx = rand.nextInt(maxSize);
038: } while (target2.getAndSet(idx));
039: target1.set(idx);
040: }
041: /***
042: int i=target1.cardinality();
043: if (i!=bitsToSet || i!=target2.cardinality()) throw new RuntimeException();
044: ***/
045: }
046:
047: public static void main(String[] args) {
048: if (args.length < 5) {
049: System.out
050: .println("BitSetTest <bitSetSize> <numSets> <numBitsSet> <testName> <iter> <impl>");
051: System.out.println(" impl => open for OpenBitSet");
052: }
053: int bitSetSize = Integer.parseInt(args[0]);
054: int numSets = Integer.parseInt(args[1]);
055: int numBitsSet = Integer.parseInt(args[2]);
056: String test = args[3];
057: int iter = Integer.parseInt(args[4]);
058: String impl = args.length > 5 ? args[5].intern() : "bit";
059:
060: BitSet[] sets = new BitSet[numSets];
061: OpenBitSet[] osets = new OpenBitSet[numSets];
062:
063: for (int i = 0; i < numSets; i++) {
064: sets[i] = new BitSet(bitSetSize);
065: osets[i] = new OpenBitSet(bitSetSize);
066: randomSets(bitSetSize, numBitsSet, sets[i], osets[i]);
067: }
068:
069: BitSet bs = new BitSet(bitSetSize);
070: OpenBitSet obs = new OpenBitSet(bitSetSize);
071: randomSets(bitSetSize, numBitsSet, bs, obs);
072:
073: int ret = 0;
074:
075: long start = System.currentTimeMillis();
076:
077: if ("union".equals(test)) {
078: for (int it = 0; it < iter; it++) {
079: for (int i = 0; i < numSets; i++) {
080: if (impl == "open") {
081: OpenBitSet other = osets[i];
082: obs.union(other);
083: } else {
084: BitSet other = sets[i];
085: bs.or(other);
086: }
087: }
088: }
089: }
090:
091: if ("cardinality".equals(test)) {
092: for (int it = 0; it < iter; it++) {
093: for (int i = 0; i < numSets; i++) {
094: if (impl == "open") {
095: ret += osets[i].cardinality();
096: } else {
097: ret += sets[i].cardinality();
098: }
099: }
100: }
101: }
102:
103: if ("get".equals(test)) {
104: for (int it = 0; it < iter; it++) {
105: for (int i = 0; i < numSets; i++) {
106: if (impl == "open") {
107: OpenBitSet oset = osets[i];
108: for (int k = 0; k < bitSetSize; k++)
109: if (oset.fastGet(k))
110: ret++;
111: } else {
112: BitSet bset = sets[i];
113: for (int k = 0; k < bitSetSize; k++)
114: if (bset.get(k))
115: ret++;
116: }
117: }
118: }
119: }
120:
121: if ("icount".equals(test)) {
122: for (int it = 0; it < iter; it++) {
123: for (int i = 0; i < numSets - 1; i++) {
124: if (impl == "open") {
125: OpenBitSet a = osets[i];
126: OpenBitSet b = osets[i + 1];
127: ret += OpenBitSet.intersectionCount(a, b);
128: } else {
129: BitSet a = sets[i];
130: BitSet b = sets[i + 1];
131: BitSet newset = (BitSet) a.clone();
132: newset.and(b);
133: ret += newset.cardinality();
134: }
135: }
136: }
137: }
138:
139: if ("clone".equals(test)) {
140: for (int it = 0; it < iter; it++) {
141: for (int i = 0; i < numSets; i++) {
142: if (impl == "open") {
143: osets[i] = (OpenBitSet) osets[i].clone();
144: } else {
145: sets[i] = (BitSet) sets[i].clone();
146: }
147: }
148: }
149: }
150:
151: if ("nextSetBit".equals(test)) {
152: for (int it = 0; it < iter; it++) {
153: for (int i = 0; i < numSets; i++) {
154: if (impl == "open") {
155: final OpenBitSet set = osets[i];
156: for (int next = set.nextSetBit(0); next >= 0; next = set
157: .nextSetBit(next + 1)) {
158: ret += next;
159: }
160: } else {
161: final BitSet set = sets[i];
162: for (int next = set.nextSetBit(0); next >= 0; next = set
163: .nextSetBit(next + 1)) {
164: ret += next;
165: }
166: }
167: }
168: }
169: }
170:
171: if ("iterator".equals(test)) {
172: for (int it = 0; it < iter; it++) {
173: for (int i = 0; i < numSets; i++) {
174: if (impl == "open") {
175: final OpenBitSet set = osets[i];
176: final BitSetIterator iterator = new BitSetIterator(
177: set);
178: for (int next = iterator.next(); next >= 0; next = iterator
179: .next()) {
180: ret += next;
181: }
182: } else {
183: final BitSet set = sets[i];
184: for (int next = set.nextSetBit(0); next >= 0; next = set
185: .nextSetBit(next + 1)) {
186: ret += next;
187: }
188: }
189: }
190: }
191: }
192:
193: long end = System.currentTimeMillis();
194: System.out.println("ret=" + ret);
195: System.out.println("TIME=" + (end - start));
196:
197: }
198:
199: }
|