forked from bage2014/study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilderBuffer.java
More file actions
73 lines (63 loc) · 1.54 KB
/
Copy pathBuilderBuffer.java
File metadata and controls
73 lines (63 loc) · 1.54 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
62
63
64
65
66
67
68
69
70
71
72
73
package com.bage.study.java.string;
/**
* StringBuilder和StringBuffer
* @author bage
*
*/
public class BuilderBuffer {
public static void main(String[] args) {
String s = new String("dsds");
System.out.println(s.hashCode());
s = s + "dsdsd";
System.out.println(s.hashCode());
System.out.println("--------测试时间:-------------");
int n = 100000;
/**
* n = 100000;
* add:3567
addEquals:3225
appendSbd:4
appendSbf:5
*/
add(n);
addEquals(n);
appendSbd(n);
appendSbf(n);
}
private static String appendSbf(int n) {
long bf = System.currentTimeMillis();
StringBuffer str = new StringBuffer("a");
for (int i = 0; i < n; i++) {
str.append("a");
}
System.out.println("appendSbf:" + (System.currentTimeMillis() - bf));
return str.toString();
}
private static String appendSbd(int n) {
long bf = System.currentTimeMillis();
StringBuilder str = new StringBuilder("a");
for (int i = 0; i < n; i++) {
str.append("a");
}
System.out.println("appendSbd:" + (System.currentTimeMillis() - bf));
return str.toString();
}
private static String addEquals(int n) {
long bf = System.currentTimeMillis();
String str = "a";
for (int i = 0; i < n; i++) {
str += "a";
}
System.out.println("addEquals:" + (System.currentTimeMillis() - bf));
return str;
}
private static String add(int n) {
long bf = System.currentTimeMillis();
String str = "a";
for (int i = 0; i < n; i++) {
str = str + "a";
}
System.out.println("add:" + (System.currentTimeMillis() - bf));
return str;
}
}