-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestString.java
More file actions
34 lines (31 loc) · 917 Bytes
/
TestString.java
File metadata and controls
34 lines (31 loc) · 917 Bytes
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
package com.github.chapter7;
/**
* 051:当心字符串连接的性能
* @author jhys
* @date 2018/8/2
*/
public class TestString {
public static void main(String[] args) {
useString();
useStringBuilder();
}
private static void useString() {
String str = "";
long start = System.currentTimeMillis();
for (int i = 0; i < 50000; i++) {
str += i;
}
// System.out.println(str);
System.out.println(System.currentTimeMillis() - start);
}
//StringBuilder 字符串变量(非线程安全)
private static void useStringBuilder() {
StringBuilder str = new StringBuilder();
long start = System.currentTimeMillis();
for (int i = 0; i < 50000; i++) {
str.append(i);
}
// System.out.println(str);
System.out.println(System.currentTimeMillis() - start);
}
}