-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPerformance.java
More file actions
61 lines (55 loc) · 1.96 KB
/
Copy pathTestPerformance.java
File metadata and controls
61 lines (55 loc) · 1.96 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.demo;
/**
* @author ManhKM on 2/14/2022
* @project Java-String
*/
public class TestPerformance {
public static void main(String[] args) {
/**
* Làm việc với String.
*/
String str ="";
long time = System.currentTimeMillis();
for (int i = 0 ; i < 100000; i++){
str+="add string ";
}
System.out.println("String: " + (System.currentTimeMillis() - time) + " ms");
/**
* Làm việc với StringBuilder:
*/
time = System.currentTimeMillis();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0 ; i < 100000; i++){
stringBuilder.append("append string");
}
System.out.println("StringBuilder 100K: "+(System.currentTimeMillis() - time) + " ms");
/**
* Làm việc với StringBuffer:
*/
time = System.currentTimeMillis();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0 ; i < 100000; i++){
stringBuffer.append("append string ");
}
System.out.println("StringBuffer 100K: "+(System.currentTimeMillis() - time) + " ms");
time = System.currentTimeMillis();
StringBuilder stringBuilder1 = new StringBuilder();
for (int i = 0 ; i < 1000000; i++){
stringBuilder1.append("append string");
}
System.out.println("StringBuilder 1M: "+(System.currentTimeMillis() - time) + " ms");
time = System.currentTimeMillis();
StringBuffer stringBuffer1 = new StringBuffer();
for (int i = 0 ; i < 1000000; i++){
stringBuffer1.append("append string ");
}
System.out.println("StringBuffer 1M: "+(System.currentTimeMillis() - time) + " ms");
/**
* String: 74492 ms
* StringBuilder 100K: 4 ms
* StringBuffer 100K: 6 ms
* StringBuilder 1M: 27 ms
* StringBuffer 1M: 27 ms
*/
}
}