01: package org.jgroups.tests;
02:
03: /**
04: * @author Bela Ban
05: * @version $Id: StringTest.java,v 1.1 2005/04/20 10:27:04 belaban Exp $
06: */
07: public class StringTest {
08: final int NUM = 1000000;
09: long start, stop;
10:
11: public static void main(String[] args) {
12: new StringTest().start();
13: }
14:
15: private void start() {
16: rawStringsWithObjects();
17: rawStringsWithLiterals();
18: stringBuffer();
19: }
20:
21: private void rawStringsWithObjects() {
22: String result = null;
23: String a = "a", b = "b", c = "c", d = "d";
24: long time = System.currentTimeMillis();
25: start = System.currentTimeMillis();
26: for (int i = 0; i < NUM; i++) {
27: result = a + b + c + d + "ecdsfh" + time;
28: }
29: stop = System.currentTimeMillis();
30: System.out.println("total time for rawStringsWithObjects(): "
31: + (stop - start));
32: System.out.println("result=" + result);
33: }
34:
35: private void rawStringsWithLiterals() {
36: String result = null;
37: long time = System.currentTimeMillis();
38: start = System.currentTimeMillis();
39: for (int i = 0; i < NUM; i++) {
40: result = "a" + "b" + "c" + "d" + "ecdsfh" + time; // needs runtime resolution
41: // result="a" + "b" + "c" + "d" + "ecdsfh" + 322463; // is concatenated at *compile time*
42: }
43: stop = System.currentTimeMillis();
44: System.out.println("total time for rawStringsWithLiterals(): "
45: + (stop - start));
46: System.out.println("result=" + result);
47: }
48:
49: private void stringBuffer() {
50: String result = null;
51: StringBuffer sb;
52: long time = System.currentTimeMillis();
53: start = System.currentTimeMillis();
54: for (int i = 0; i < NUM; i++) {
55: sb = new StringBuffer("a");
56: sb.append("b").append("c").append("d").append("ecdsfh")
57: .append(time);
58: result = sb.toString();
59: }
60: stop = System.currentTimeMillis();
61: System.out.println("total time for stringBuffer(): "
62: + (stop - start));
63: System.out.println("result=" + result);
64: }
65: }
|