-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathStringExample.java
More file actions
38 lines (32 loc) · 1.41 KB
/
StringExample.java
File metadata and controls
38 lines (32 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* This example shows that StringBuilder may be faster than String in
* certain circumstances. Since you cannot change a String object, you
* should consider using StringBuilder in cases when you want to change
* the String.
*
* The sample below changes the "helloX" to "hello!". The first method uses String.
* The second method uses StringBuilder. This operation is repeated millions of times
* and the elapsed time is displayed. Note how much faster StringBuilder is.
*/
public class StringExample {
public static void main(String[] args) {
String s = "helloX";
StringBuilder sb = new StringBuilder("helloX");
long start, end;
int reps = 50000000;
/** use String to change the character X to the character ! */
start = System.currentTimeMillis();
for (int i=0; i < reps; i++) {
s = s.substring(0,5) + "!";
}
end = System.currentTimeMillis();
System.out.println("With String, the elapsed time in seconds is " + ((float)(end-start)/1000));
/** use StringBuilder to change the character X to the character ! */
start = System.currentTimeMillis();
for (int i=0; i < reps; i++) {
sb.setCharAt(5, '!');
}
end = System.currentTimeMillis();
System.out.println("With StringBuilder, the elapsed time in seconds is " + ((float)(end-start)/1000));
}
}