forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSbDemo.java
More file actions
37 lines (32 loc) · 1.09 KB
/
SbDemo.java
File metadata and controls
37 lines (32 loc) · 1.09 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
/**
* @program JavaBooks
* @description: String StringBuffer StringBuilder
* @author: mf
* @create: 2020/02/07 19:13
*/
package com.strings;
public class SbDemo {
public static void main(String[] args) {
// String
String str = "hello";
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
str += i; // 创建多少个对象,,
}
System.out.println("String: " + (System.currentTimeMillis() - start));
// StringBuffer
StringBuffer sb = new StringBuffer("hello");
long start1 = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
sb.append(i);
}
System.out.println("StringBuffer: " + (System.currentTimeMillis() - start1));
// StringBuilder
StringBuilder stringBuilder = new StringBuilder("hello");
long start2 = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
stringBuilder.append(i);
}
System.out.println("StringBuilder: " + (System.currentTimeMillis() - start2));
}
}