Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dd3587c

Browse filesBrowse files
Update why-is-printing-b-dramatically-slower-than-printing.md
1 parent 92e3f02 commit dd3587c
Copy full SHA for dd3587c

File tree

Expand file treeCollapse file tree

1 file changed

+51
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+51
-1
lines changed

‎contents/why-is-printing-b-dramatically-slower-than-printing.md

Copy file name to clipboardExpand all lines: contents/why-is-printing-b-dramatically-slower-than-printing.md
+51-1Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
- 我在Netbeans 7.2中运行测试,由控制台显示输出
5252
- 我使用了`System.nanoTime()`来计算时间
5353

54-
## 解答
54+
## 解答一
5555

5656
*纯粹的推测*是因为你使用的终端尝试使用[单词换行][1]而不是字符换行,并且它认为`B`是一个单词而`#`却不是。所以当它到达行尾并且寻找一个换行的地方的时候,如果是`#`就可以马上换行;而如果是`B`,它则需要花更长时间搜索,因为可能会有更多的内容才能换行(在某些终端会非常费时,比如说它会先输出退格再输出空格去覆盖被换行的那部分字符)。
5757

@@ -61,4 +61,54 @@
6161
[1]: http://en.wikipedia.org/wiki/Word_wrap
6262

6363

64+
##解答二
65+
66+
我用Eclipse和Netbeans 8.0.2做了测试,他们的Java版本都是1.8;我用了`System.nanoTime()`来计时。
67+
68+
##Eclipse:
69+
70+
我得到了**用时相同的结果** - 大约**1.564秒**
71+
72+
##Netbeans:
73+
74+
* 使用"#": **1.536秒**
75+
* 使用"B": **44.164秒**
76+
77+
所以看起来像是Netbeans输出到控制台的性能问题。
78+
79+
在做了更多研究以后我发现问题所在是Netbeans [换行][1] 的最大缓存(这并不限于`System.out.println`命令),参见以下代码:
80+
81+
for (int i = 0; i < 1000; i++) {
82+
long t1 = System.nanoTime();
83+
System.out.print("BBB......BBB"); \\<-contain 1000 "B"
84+
long t2 = System.nanoTime();
85+
System.out.println(t2-t1);
86+
System.out.println("");
87+
}
88+
89+
每一个循环所花费的时间都不到1毫秒,除了 **每第五个循环**会花掉大约225毫秒。像这样(单位是毫秒):
90+
91+
BBB...31744
92+
BBB...31744
93+
BBB...31744
94+
BBB...31744
95+
BBB...226365807
96+
BBB...31744
97+
BBB...31744
98+
BBB...31744
99+
BBB...31744
100+
BBB...226365807
101+
.
102+
.
103+
.
104+
105+
以此类推。
106+
107+
##总结:
108+
109+
1. 使用Eclipse打印“B”完全没有问题
110+
1. Netbeans有换行的问题但是可以被解决(因为在Eclipse并没有这个问题)(而不用在B后面添加空格(“B ”))。
111+
112+
[1]: http://en.wikipedia.org/wiki/Line_wrap_and_word_wrap
113+
64114
stackoverflow原址:http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.