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 c59b831

Browse filesBrowse files
committed
Merge pull request giantray#40 from zhongjianluxian/master
添加翻译:为什么打印“B”会明显的比打印“#”慢,以及如何用0向左补齐一个整数
2 parents 7856e30 + 463f633 commit c59b831
Copy full SHA for c59b831

File tree

Expand file treeCollapse file tree

2 files changed

+132
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+132
-0
lines changed
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 如何用0向左补齐一个整数?
2+
3+
## 问题
4+
5+
在Java中如何把一个整数转化为字符串并且用0向左补齐呢?
6+
7+
我基本上是希望把一个不大于9999的整数用0补齐 (比如说1=“0001”)。
8+
9+
## 解答
10+
11+
String.format("%05d", yournumber);
12+
13+
可以将一个五位数用0补齐。
14+
15+
16+
https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
17+
18+
Stackoverflow链接:http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
+114Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# 为什么打印“B”会明显的比打印“#”慢
2+
3+
## 问题
4+
5+
我生成了两个`1000`x`1000`的矩阵:
6+
7+
第一个矩阵:`O``#`
8+
第二个矩阵:`O``B`
9+
10+
使用如下的代码,生成第一个矩阵需要8.52秒:
11+
12+
Random r = new Random();
13+
for (int i = 0; i < 1000; i++) {
14+
for (int j = 0; j < 1000; j++) {
15+
if(r.nextInt(4) == 0) {
16+
System.out.print("O");
17+
} else {
18+
System.out.print("#");
19+
}
20+
}
21+
22+
System.out.println("");
23+
}
24+
25+
26+
而使用这段代码,生成第二个矩阵花费了259.152秒:
27+
28+
Random r = new Random();
29+
for (int i = 0; i < 1000; i++) {
30+
for (int j = 0; j < 1000; j++) {
31+
if(r.nextInt(4) == 0) {
32+
System.out.print("O");
33+
} else {
34+
System.out.print("B"); //only line changed
35+
}
36+
}
37+
38+
System.out.println("");
39+
}
40+
41+
如此大的运行时间差异的背后究竟是什么原因呢?
42+
43+
---
44+
45+
正如评论中所建议的,只打印`System.out.print("#");`用时7.8871秒,而`System.out.print("B");`则给出`still printing...`
46+
47+
另外有人指出这段代码对他们来说是正常的, 我使用了[Ideone.com](http://ideone.com),这两段代码的执行速度是相同的。
48+
49+
测试条件:
50+
51+
- 我在Netbeans 7.2中运行测试,由控制台显示输出
52+
- 我使用了`System.nanoTime()`来计算时间
53+
54+
## 解答一
55+
56+
*纯粹的推测*是因为你使用的终端尝试使用[单词换行][1]而不是字符换行,并且它认为`B`是一个单词而`#`却不是。所以当它到达行尾并且寻找一个换行的地方的时候,如果是`#`就可以马上换行;而如果是`B`,它则需要花更长时间搜索,因为可能会有更多的内容才能换行(在某些终端会非常费时,比如说它会先输出退格再输出空格去覆盖被换行的那部分字符)。
57+
58+
但这都只是纯粹的推测。
59+
60+
61+
[1]: http://en.wikipedia.org/wiki/Word_wrap
62+
63+
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+
114+
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.