KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > StringTest


1 package org.jgroups.tests;
2
3 /**
4  * @author Bela Ban
5  * @version $Id: StringTest.java,v 1.1 2005/04/20 10:27:04 belaban Exp $
6  */

7 public class StringTest {
8     final int NUM=1000000;
9     long start, stop;
10
11     public static void main(String JavaDoc[] args) {
12         new StringTest().start();
13     }
14
15     private void start() {
16         rawStringsWithObjects();
17         rawStringsWithLiterals();
18         stringBuffer();
19     }
20
21
22     private void rawStringsWithObjects() {
23         String JavaDoc result=null;
24         String JavaDoc a="a", b="b", c="c", d="d";
25         long time=System.currentTimeMillis();
26         start=System.currentTimeMillis();
27         for(int i=0; i < NUM; i++) {
28             result=a + b + c + d + "ecdsfh" + time;
29         }
30         stop=System.currentTimeMillis();
31         System.out.println("total time for rawStringsWithObjects(): " + (stop-start));
32         System.out.println("result=" + result);
33     }
34
35     private void rawStringsWithLiterals() {
36         String JavaDoc 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(): " + (stop-start));
45         System.out.println("result=" + result);
46     }
47
48     private void stringBuffer() {
49         String JavaDoc result=null;
50         StringBuffer JavaDoc sb;
51         long time=System.currentTimeMillis();
52         start=System.currentTimeMillis();
53         for(int i=0; i < NUM; i++) {
54             sb=new StringBuffer JavaDoc("a");
55             sb.append("b").append("c").append("d").append("ecdsfh").append(time);
56             result=sb.toString();
57         }
58         stop=System.currentTimeMillis();
59         System.out.println("total time for stringBuffer(): " + (stop-start));
60         System.out.println("result=" + result);
61     }
62 }
63
Popular Tags