|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.lang3; |
| 18 | + |
| 19 | +import org.openjdk.jmh.annotations.Benchmark; |
| 20 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 21 | +import org.openjdk.jmh.annotations.Fork; |
| 22 | +import org.openjdk.jmh.annotations.Measurement; |
| 23 | +import org.openjdk.jmh.annotations.Mode; |
| 24 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 25 | +import org.openjdk.jmh.annotations.Scope; |
| 26 | +import org.openjdk.jmh.annotations.State; |
| 27 | +import org.openjdk.jmh.annotations.Warmup; |
| 28 | +import org.openjdk.jmh.infra.Blackhole; |
| 29 | + |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +import static org.apache.commons.lang3.StringUtils.isEmpty; |
| 33 | + |
| 34 | +/** |
| 35 | + * Test to show whether using BitSet for removeAll() methods is faster than using HashSet. |
| 36 | + */ |
| 37 | +@BenchmarkMode(Mode.AverageTime) |
| 38 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 39 | +@State(Scope.Thread) |
| 40 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 41 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 42 | +@Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"}) |
| 43 | +public class StringUtilsWrapTest { |
| 44 | + static String[] strings = buildStrings(); |
| 45 | + |
| 46 | + private static String[] buildStrings() { |
| 47 | + String[] res = new String[128 * 128]; |
| 48 | + for (int i = 0; i < 128; i++) { |
| 49 | + for (int j = 0; j < 128; j++) { |
| 50 | + StringBuilder stringBuilder = new StringBuilder(); |
| 51 | + stringBuilder.append((char) i); |
| 52 | + stringBuilder.append((char) j); |
| 53 | + res[i * 128 + j] = stringBuilder.toString(); |
| 54 | + } |
| 55 | + } |
| 56 | + return res; |
| 57 | + } |
| 58 | + |
| 59 | + @Benchmark |
| 60 | + public String test00Old() { |
| 61 | + return wrapOld("a", "b"); |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public String test00New() { |
| 66 | + return wrapNew("a", "b"); |
| 67 | + } |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + public String test01Old() { |
| 71 | + return wrapOld("aaaaaaaaaa", "b"); |
| 72 | + } |
| 73 | + |
| 74 | + @Benchmark |
| 75 | + public String test01New() { |
| 76 | + return wrapNew("aaaaaaaaaa", "b"); |
| 77 | + } |
| 78 | + |
| 79 | + @Benchmark |
| 80 | + public String test10Old() { |
| 81 | + return wrapIfMissingOld("aaaaaaaaaa", "b"); |
| 82 | + } |
| 83 | + |
| 84 | + @Benchmark |
| 85 | + public String test10New() { |
| 86 | + return wrapIfMissingNew("aaaaaaaaaa", "b"); |
| 87 | + } |
| 88 | + |
| 89 | + @Benchmark |
| 90 | + public String test11Old() { |
| 91 | + return wrapIfMissingOld("ab", "b"); |
| 92 | + } |
| 93 | + |
| 94 | + @Benchmark |
| 95 | + public String test11New() { |
| 96 | + return wrapIfMissingNew("ab", "b"); |
| 97 | + } |
| 98 | + |
| 99 | + @Benchmark |
| 100 | + public String test12ld() { |
| 101 | + return wrapIfMissingOld("bb", "b"); |
| 102 | + } |
| 103 | + |
| 104 | + @Benchmark |
| 105 | + public String test12New() { |
| 106 | + return wrapIfMissingNew("bb", "b"); |
| 107 | + } |
| 108 | + |
| 109 | + @Benchmark |
| 110 | + public String test20Old() { |
| 111 | + return wrapIfMissingOld("aaaaaaaaaa", 'b'); |
| 112 | + } |
| 113 | + |
| 114 | + @Benchmark |
| 115 | + public String test20New() { |
| 116 | + return wrapIfMissingNew("aaaaaaaaaa", 'b'); |
| 117 | + } |
| 118 | + |
| 119 | + @Benchmark |
| 120 | + public String test21Old() { |
| 121 | + return wrapIfMissingOld("ab", 'b'); |
| 122 | + } |
| 123 | + |
| 124 | + @Benchmark |
| 125 | + public String test21New() { |
| 126 | + return wrapIfMissingNew("ab", 'b'); |
| 127 | + } |
| 128 | + |
| 129 | + @Benchmark |
| 130 | + public String test22Old() { |
| 131 | + return wrapIfMissingOld("bb", 'b'); |
| 132 | + } |
| 133 | + |
| 134 | + @Benchmark |
| 135 | + public String test22New() { |
| 136 | + return wrapIfMissingNew("bb", 'b'); |
| 137 | + } |
| 138 | + |
| 139 | + @Benchmark |
| 140 | + public void testsOld(Blackhole blackhole) { |
| 141 | + for (int i = 0; i < 128; i++) { |
| 142 | + for (String au : strings) { |
| 143 | + blackhole.consume(wrapIfMissingOld(au, (char) i)); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Benchmark |
| 149 | + public void testsNew(Blackhole blackhole) { |
| 150 | + for (int i = 0; i < 128; i++) { |
| 151 | + for (String au : strings) { |
| 152 | + blackhole.consume(wrapIfMissingNew(au, (char) i)); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + //----- |
| 158 | + |
| 159 | + public static String wrapOld(final String str, final String wrapWith) { |
| 160 | + |
| 161 | + if (isEmpty(str) || isEmpty(wrapWith)) { |
| 162 | + return str; |
| 163 | + } |
| 164 | + |
| 165 | + return wrapWith.concat(str).concat(wrapWith); |
| 166 | + } |
| 167 | + |
| 168 | + public static String wrapNew(final String str, final String wrapWith) { |
| 169 | + |
| 170 | + if (isEmpty(str) || isEmpty(wrapWith)) { |
| 171 | + return str; |
| 172 | + } |
| 173 | + |
| 174 | + return wrapWith + str + wrapWith; |
| 175 | + } |
| 176 | + |
| 177 | + public static String wrapIfMissingOld(final String str, final char wrapWith) { |
| 178 | + if (isEmpty(str) || wrapWith == CharUtils.NUL) { |
| 179 | + return str; |
| 180 | + } |
| 181 | + final boolean wrapStart = str.charAt(0) != wrapWith; |
| 182 | + final boolean wrapEnd = str.charAt(str.length() - 1) != wrapWith; |
| 183 | + if (!wrapStart && !wrapEnd) { |
| 184 | + return str; |
| 185 | + } |
| 186 | + |
| 187 | + final StringBuilder builder = new StringBuilder(str.length() + 2); |
| 188 | + if (wrapStart) { |
| 189 | + builder.append(wrapWith); |
| 190 | + } |
| 191 | + builder.append(str); |
| 192 | + if (wrapEnd) { |
| 193 | + builder.append(wrapWith); |
| 194 | + } |
| 195 | + return builder.toString(); |
| 196 | + } |
| 197 | + |
| 198 | + public static String wrapIfMissingNew(final String str, final char wrapWith) { |
| 199 | + if (isEmpty(str) || wrapWith == CharUtils.NUL) { |
| 200 | + return str; |
| 201 | + } |
| 202 | + final boolean wrapStart = str.charAt(0) != wrapWith; |
| 203 | + final boolean wrapEnd = str.charAt(str.length() - 1) != wrapWith; |
| 204 | + if (wrapStart) { |
| 205 | + if (wrapEnd) { |
| 206 | + return wrapWith + str + wrapWith; |
| 207 | + } else { |
| 208 | + return wrapWith + str; |
| 209 | + } |
| 210 | + } else { |
| 211 | + if (wrapEnd) { |
| 212 | + return str + wrapWith; |
| 213 | + } else { |
| 214 | + return str; |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + public static String wrapIfMissingOld(final String str, final String wrapWith) { |
| 220 | + if (isEmpty(str) || isEmpty(wrapWith)) { |
| 221 | + return str; |
| 222 | + } |
| 223 | + |
| 224 | + final boolean wrapStart = !str.startsWith(wrapWith); |
| 225 | + final boolean wrapEnd = !str.endsWith(wrapWith); |
| 226 | + if (!wrapStart && !wrapEnd) { |
| 227 | + return str; |
| 228 | + } |
| 229 | + |
| 230 | + final StringBuilder builder = new StringBuilder(str.length() + wrapWith.length() + wrapWith.length()); |
| 231 | + if (wrapStart) { |
| 232 | + builder.append(wrapWith); |
| 233 | + } |
| 234 | + builder.append(str); |
| 235 | + if (wrapEnd) { |
| 236 | + builder.append(wrapWith); |
| 237 | + } |
| 238 | + return builder.toString(); |
| 239 | + } |
| 240 | + |
| 241 | + public static String wrapIfMissingNew(final String str, final String wrapWith) { |
| 242 | + if (isEmpty(str) || isEmpty(wrapWith)) { |
| 243 | + return str; |
| 244 | + } |
| 245 | + |
| 246 | + final boolean wrapStart = !str.startsWith(wrapWith); |
| 247 | + final boolean wrapEnd = !str.endsWith(wrapWith); |
| 248 | + if (wrapStart) { |
| 249 | + if (wrapEnd) { |
| 250 | + return wrapWith + str + wrapWith; |
| 251 | + } else { |
| 252 | + return wrapWith + str; |
| 253 | + } |
| 254 | + } else { |
| 255 | + if (wrapEnd) { |
| 256 | + return str + wrapWith; |
| 257 | + } else { |
| 258 | + return str; |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments