1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ********************************************************************** 5 * Copyright (c) 2002-2007, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ********************************************************************** 8 */ 9 package com.ibm.icu.dev.test.perf; 10 import com.ibm.icu.text.*; 11 import java.util.HashSet; 12 import java.util.Iterator; 13 14 /** 15 * A class for testing UnicodeSet performance. 16 * 17 * @author Alan Liu 18 * @since ICU 2.4 19 */ 20 public class UnicodeSetPerf extends PerfTest { 21 22 String pattern; 23 UnicodeSet testChars; 24 UnicodeSetIterator it; 25 UnicodeSet us; 26 HashSet hs; 27 28 public static void main(String[] args) throws Exception { 29 new UnicodeSetPerf().run(args); 30 } 31 32 protected void setup(String[] args) { 33 // We only take one argument, the pattern 34 if (args.length != 1) { 35 throw new RuntimeException("Please supply UnicodeSet pattern"); 36 } 37 38 pattern = args[0]; 39 testChars = new UnicodeSet(pattern); 40 it = new UnicodeSetIterator(testChars); 41 us = new UnicodeSet(); 42 hs = new HashSet(); 43 } 44 45 PerfTest.Function testUnicodeSetAdd() { 46 return new PerfTest.Function() { 47 public void call() { 48 us.clear(); 49 it.reset(); 50 int n=0; 51 while (it.nextRange()) { 52 for (int cp = it.codepoint; cp <= it.codepointEnd; ++cp) { 53 us.add(cp); 54 ++n; 55 } 56 } 57 } 58 59 public long getOperationsPerIteration() { 60 return testChars.size(); 61 } 62 }; 63 } 64 65 PerfTest.Function testHashSetAdd() { 66 return new PerfTest.Function() { 67 public void call() { 68 hs.clear(); 69 it.reset(); 70 int n=0; 71 while (it.nextRange()) { 72 for (int cp = it.codepoint; cp <= it.codepointEnd; ++cp) { 73 hs.add(new Integer(cp)); 74 ++n; 75 } 76 } 77 } 78 79 public long getOperationsPerIteration() { 80 return testChars.size(); 81 } 82 }; 83 } 84 85 PerfTest.Function testUnicodeSetContains() { 86 us.clear(); 87 us.set(testChars); 88 89 return new PerfTest.Function() { 90 public void call() { 91 int temp = 0; 92 for (int cp = 0; cp <= 0x10FFFF; ++cp) { 93 if (us.contains(cp)) { 94 temp += cp; 95 } 96 } 97 } 98 99 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(testChars); 132 while (uit.next()) { 133 temp += uit.codepoint; 134 } 135 } 136 137 public long getOperationsPerIteration() { 138 return testChars.size(); 139 } 140 }; 141 } 142 143 PerfTest.Function testHashSetIterate() { 144 hs.clear(); 145 it.reset(); 146 while (it.next()) { 147 hs.add(new Integer(it.codepoint)); 148 } 149 return new PerfTest.Function() { 150 public void call() { 151 int temp = 0; 152 Iterator itr = hs.iterator(); 153 while (itr.hasNext()) { 154 temp += ((Integer)itr.next()).intValue(); 155 } 156 } 157 158 public long getOperationsPerIteration() { 159 return testChars.size(); 160 } 161 }; 162 } 163 } 164