001: /*
002: **********************************************************************
003: * Copyright (c) 2002-2004, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **********************************************************************
006: */
007: package com.ibm.icu.dev.test.perf;
008:
009: import com.ibm.icu.text.*;
010: import java.util.HashSet;
011: import java.util.Iterator;
012:
013: /**
014: * A class for testing UnicodeSet performance.
015: *
016: * @author Alan Liu
017: * @since ICU 2.4
018: */
019: public class UnicodeSetPerf extends PerfTest {
020:
021: String pattern;
022: UnicodeSet testChars;
023: UnicodeSetIterator it;
024: UnicodeSet us;
025: HashSet hs;
026:
027: public static void main(String[] args) throws Exception {
028: new UnicodeSetPerf().run(args);
029: }
030:
031: protected void setup(String[] args) {
032: // We only take one argument, the pattern
033: if (args.length != 1) {
034: throw new RuntimeException(
035: "Please supply UnicodeSet pattern");
036: }
037:
038: pattern = args[0];
039: testChars = new UnicodeSet(pattern);
040: it = new UnicodeSetIterator(testChars);
041: us = new UnicodeSet();
042: hs = new HashSet();
043: }
044:
045: PerfTest.Function testUnicodeSetAdd() {
046: return new PerfTest.Function() {
047: public void call() {
048: us.clear();
049: it.reset();
050: int n = 0;
051: while (it.nextRange()) {
052: for (int cp = it.codepoint; cp <= it.codepointEnd; ++cp) {
053: us.add(cp);
054: ++n;
055: }
056: }
057: }
058:
059: public long getOperationsPerIteration() {
060: return testChars.size();
061: }
062: };
063: }
064:
065: PerfTest.Function testHashSetAdd() {
066: return new PerfTest.Function() {
067: public void call() {
068: hs.clear();
069: it.reset();
070: int n = 0;
071: while (it.nextRange()) {
072: for (int cp = it.codepoint; cp <= it.codepointEnd; ++cp) {
073: hs.add(new Integer(cp));
074: ++n;
075: }
076: }
077: }
078:
079: public long getOperationsPerIteration() {
080: return testChars.size();
081: }
082: };
083: }
084:
085: PerfTest.Function testUnicodeSetContains() {
086: us.clear();
087: us.set(testChars);
088:
089: return new PerfTest.Function() {
090: public void call() {
091: int temp = 0;
092: for (int cp = 0; cp <= 0x10FFFF; ++cp) {
093: if (us.contains(cp)) {
094: temp += cp;
095: }
096: }
097: }
098:
099: public long getOperationsPerIteration() {
100: return 0x110000;
101: }
102: };
103: }
104:
105: PerfTest.Function testHashSetContains() {
106: hs.clear();
107: it.reset();
108: while (it.next()) {
109: hs.add(new Integer(it.codepoint));
110: }
111: return new PerfTest.Function() {
112: public void call() {
113: int temp = 0;
114: for (int cp = 0; cp <= 0x10FFFF; ++cp) {
115: if (hs.contains(new Integer(cp))) {
116: temp += cp;
117: }
118: }
119: }
120:
121: public long getOperationsPerIteration() {
122: return 0x110000;
123: }
124: };
125: }
126:
127: PerfTest.Function testUnicodeSetIterate() {
128: return new PerfTest.Function() {
129: public void call() {
130: int temp = 0;
131: UnicodeSetIterator uit = new UnicodeSetIterator(
132: testChars);
133: while (uit.next()) {
134: temp += uit.codepoint;
135: }
136: }
137:
138: public long getOperationsPerIteration() {
139: return testChars.size();
140: }
141: };
142: }
143:
144: PerfTest.Function testHashSetIterate() {
145: hs.clear();
146: it.reset();
147: while (it.next()) {
148: hs.add(new Integer(it.codepoint));
149: }
150: return new PerfTest.Function() {
151: public void call() {
152: int temp = 0;
153: Iterator it = hs.iterator();
154: while (it.hasNext()) {
155: temp += ((Integer) it.next()).intValue();
156: }
157: }
158:
159: public long getOperationsPerIteration() {
160: return testChars.size();
161: }
162: };
163: }
164: }
|