1 9 package javolution; 10 11 import javolution.context.PoolContext; 12 import javolution.lang.MathLib; 13 import javolution.text.Text; 14 import javolution.text.TextBuilder; 15 import javolution.text.TypeFormat; 16 17 23 final class PerfText extends Javolution implements Runnable { 24 25 static volatile int COUNT = 1000; 27 private final String STRING = "Concatenates this line 1000 times (resulting in a text of about 80,000 characters)"; 28 29 private final Text TEXT = Text.valueOf(STRING); 30 31 private final Text ONE_CHAR = Text.valueOf('X'); 32 33 36 public void run() { 37 println("//////////////////////////////"); 38 println("// Package: javolution.text //"); 39 println("//////////////////////////////"); 40 println(""); 41 42 println("-- Primitive types formatting --"); 43 println(""); 44 int n = 100000; 45 int ii = MathLib.random(Integer.MIN_VALUE, Integer.MAX_VALUE); 46 long ll = MathLib.random(Long.MIN_VALUE, Long.MAX_VALUE); 47 48 print("StringBuffer.append(int): "); 49 for (int j=0; j < 10; j++) { 50 StringBuffer tmp = new StringBuffer (); 51 startTime(); 52 for (int i = 0; i < n; i++) { 53 tmp.setLength(0); 54 tmp.append(ii); 55 } 56 keepBestTime(n); 57 } 58 println(endTime()); 59 60 print("TextBuilder.append(int): "); 61 for (int j=0; j < 10; j++) { 62 TextBuilder tmp = new TextBuilder(); 63 startTime(); 64 for (int i = 0; i < n; i++) { 65 tmp.clear().append(ii); 66 } 67 keepBestTime(n); 68 if (!tmp.contentEquals(ii + "")) throw new Error (); 69 } 70 println(endTime()); 71 72 print("StringBuffer.append(long): "); 73 for (int j=0; j < 10; j++) { 74 StringBuffer tmp = new StringBuffer (); 75 startTime(); 76 for (int i = 0; i < n; i++) { 77 tmp.setLength(0); 78 tmp.append(ll); 79 } 80 keepBestTime(n); 81 } 82 println(endTime()); 83 84 print("TextBuilder.append(long): "); 85 for (int j=0; j < 10; j++) { 86 TextBuilder tmp = new TextBuilder(); 87 startTime(); 88 for (int i = 0; i < n; i++) { 89 tmp.clear().append(ll); 90 } 91 keepBestTime(n); 92 if (!tmp.contentEquals(ll + "")) throw new Error (); 93 } 94 println(endTime()); 95 96 148 println(""); 149 150 println("-- Primitive types parsing --"); 151 println(""); 152 String is = "" + ii; 153 String ls = "" + ll; 154 155 print("Integer.parseInt(String): "); 156 for (int j=0; j < 10; j++) { 157 startTime(); 158 for (int i = 0; i < n; i++) { 159 if (Integer.parseInt(is) != ii) throw new Error (); 160 } 161 keepBestTime(n); 162 } 163 println(endTime()); 164 165 print("TypeFormat.parseInt(String): "); 166 for (int j=0; j < 10; j++) { 167 startTime(); 168 for (int i = 0; i < n; i++) { 169 if (TypeFormat.parseInt(is) != ii) throw new Error (); 170 } 171 keepBestTime(n); 172 } 173 println(endTime()); 174 175 print("Long.parseLong(String): "); 176 for (int j=0; j < 10; j++) { 177 startTime(); 178 for (int i = 0; i < n; i++) { 179 if (Long.parseLong(ls) != ll) throw new Error (); 180 } 181 keepBestTime(n); 182 } 183 println(endTime()); 184 185 print("TypeFormat.parseLong(String): "); 186 for (int j=0; j < 10; j++) { 187 startTime(); 188 for (int i = 0; i < n; i++) { 189 if (TypeFormat.parseLong(ls) != ll) throw new Error (); 190 } 191 keepBestTime(n); 192 } 193 println(endTime()); 194 195 239 println(""); 240 241 println("-- String/StringBuffer versus Text --"); 242 println(""); 243 244 println("\"" + STRING + "\""); 245 246 print("String \"+\" operator: "); 247 String str = ""; 248 for (int i = 0; i < 2; i++) { 249 str = ""; 250 startTime(); 251 for (int j = COUNT; --j >= 0;) { 252 str += STRING; 253 } 254 keepBestTime(COUNT); 255 } 256 println(endTime()); 257 258 print("StringBuffer \"append\" : "); 259 for (int i = 0; i < 100; i++) { 260 StringBuffer sb = new StringBuffer (); 261 startTime(); 262 for (int j = COUNT; --j >= 0;) { 263 sb.append(STRING); 264 } 265 keepBestTime(COUNT); 266 } 267 println(endTime()); 268 269 print("Text \"concat\" (heap): "); 270 for (int i = 0; i < 100; i++) { 271 Text txt = Text.EMPTY; 272 startTime(); 273 for (int j = COUNT; --j >= 0;) { 274 txt = txt.concat(TEXT); 275 } 276 keepBestTime(COUNT); 277 } 278 println(endTime()); 279 280 print("Text \"concat\" (stack): "); 281 for (int i = 0; i < 100; i++) { 282 PoolContext.enter(); 283 Text txt = Text.EMPTY; 284 startTime(); 285 for (int j = COUNT; --j >= 0;) { 286 txt = txt.concat(TEXT); 287 } 288 keepBestTime(COUNT); 289 PoolContext.exit(); 290 } 291 println(endTime()); 292 293 println(""); 294 println("Inserts one character at random locations 1,000 times to the 80,000 characters text."); 295 296 print("StringBuffer insert: "); 297 for (int i = 0; i < 100; i++) { 298 StringBuffer sb = new StringBuffer (str); 299 startTime(); 300 for (int j = COUNT; --j >= 0;) { 301 int index = MathLib.random(0, sb.length()); 302 sb.insert(index, 'X'); 303 } 304 keepBestTime(COUNT); 305 } 306 println(endTime()); 307 308 print("Text insert (heap): "); 309 for (int i = 0; i < 100; i++) { 310 Text txt = Text.valueOf(str); 311 startTime(); 312 for (int j = COUNT; --j >= 0;) { 313 int index = MathLib.random(0, txt.length()); 314 txt = txt.insert(index, ONE_CHAR); 315 } 316 keepBestTime(COUNT); 317 } 318 println(endTime()); 319 320 print("Text insert (stack): "); 321 for (int i = 0; i < 100; i++) { 322 PoolContext.enter(); 323 Text txt = Text.valueOf(str); 324 startTime(); 325 for (int j = COUNT; --j >= 0;) { 326 int index = MathLib.random(0, txt.length()); 327 txt = txt.insert(index, ONE_CHAR); 328 } 329 keepBestTime(COUNT); 330 PoolContext.exit(); 331 } 332 println(endTime()); 333 334 println(""); 335 println("Delete 1,000 times one character at random location from the 80,000 characters text."); 336 337 print("StringBuffer delete: "); 338 for (int i = 0; i < 100; i++) { 339 StringBuffer sb = new StringBuffer (str); 340 startTime(); 341 for (int j = COUNT; --j >= 0;) { 342 int index = MathLib.random(0, sb.length() - 1); 343 sb.deleteCharAt(index); 344 } 345 keepBestTime(COUNT); 346 } 347 println(endTime()); 348 349 print("Text delete (heap): "); 350 for (int i = 0; i < 100; i++) { 351 Text txt = Text.valueOf(str); 352 startTime(); 353 for (int j = COUNT; --j >= 0;) { 354 int index = MathLib.random(0, txt.length() - 1); 355 txt = txt.delete(index, index + 1); 356 } 357 keepBestTime(COUNT); 358 } 359 println(endTime()); 360 361 print("Text delete (stack): "); 362 for (int i = 0; i < 100; i++) { 363 PoolContext.enter(); 364 Text txt = Text.valueOf(str); 365 startTime(); 366 for (int j = COUNT; --j >= 0;) { 367 int index = MathLib.random(0, txt.length() - 1); 368 txt = txt.delete(index, index + 1); 369 } 370 keepBestTime(COUNT); 371 PoolContext.exit(); 372 } 373 println(endTime()); 374 375 println(""); 376 } 377 } | Popular Tags |