Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

InputStream转String性能测试

测试代码如下

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
public static void main(String[] args) {

try {

long scannerStart = System.currentTimeMillis();
for (int i = 1; i < 500000; i++) {
scannerMethod();
}

System.out.println("scanner cost:"
+ (System.currentTimeMillis() - scannerStart) + " ms");

long streamStart = System.currentTimeMillis();

for (int i = 1; i < 500000; i++) {
streamMethod();
}
System.out.println("stream cost:"
+ (System.currentTimeMillis() - streamStart) + " ms");

} catch (Exception e) {
e.printStackTrace();
}

}

public static void scannerMethod() throws MalformedURLException, IOException{
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();

try( Scanner s = new Scanner(in) ){
s.useDelimiter("\\A").next();
}
in.close();
}


public static void streamMethod() throws IOException{
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String(in);
in.close();
}
public static String inputStream2String(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = in.read()) != -1) {
baos.write(i);
}
return baos.toString();
}

结果

1
2
scanner cost:26661 ms
stream cost:18642 ms
1
2
scanner cost:25268 ms
stream cost:17991 ms
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
74
75
76
77
public static void main(String[] args) throws MalformedURLException, IOException {

long method1 = System.currentTimeMillis();
for(int i =1 ;i<50000;i++){
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String1(in);

in.close();
}
System.out.println("method1 cost:"
+ (System.currentTimeMillis() - method1) + " ms");

long method2 = System.currentTimeMillis();
for(int i =1 ;i<50000;i++){
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String2(in);

in.close();
}
System.out.println("method2 cost:"
+ (System.currentTimeMillis() - method2) + " ms");

long method3 = System.currentTimeMillis();
for(int i =1 ;i<50000;i++){
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String3(in);

in.close();
}
System.out.println("method3 cost:"
+ (System.currentTimeMillis() - method3) + " ms");
}

public static String inputStream2String1(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

public static String inputStream2String2(InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}

public static String inputStream2String3(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1) {
baos.write(i);
}
return baos.toString();
}

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
method1 cost:3086 ms
method2 cost:2056 ms
method3 cost:1813 ms

method1 cost:2986 ms
method2 cost:2045 ms
method3 cost:2156 ms

method1 cost:2921 ms
method2 cost:2047 ms
method3 cost:1795 ms

method1 cost:2980 ms
method2 cost:2015 ms
method3 cost:1744 ms