Java 为什么打印“B”比打印“#”慢得多?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21947452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is printing "B" dramatically slower than printing "#"?
提问by Kuba Spatny
I generated two matrices of 1000
x 1000
:
我生成了1000
x 的两个矩阵1000
:
First Matrix: O
and #
.
Second Matrix: O
and B
.
第一个矩阵:O
和#
。
第二个矩阵:O
和B
。
Using the following code, the first matrix took 8.52 seconds to complete:
使用以下代码,完成第一个矩阵需要 8.52 秒:
Random r = new Random();
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if(r.nextInt(4) == 0) {
System.out.print("O");
} else {
System.out.print("#");
}
}
System.out.println("");
}
With this code, the second matrix took 259.152 seconds to complete:
使用此代码,第二个矩阵需要 259.152 秒才能完成:
Random r = new Random();
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if(r.nextInt(4) == 0) {
System.out.print("O");
} else {
System.out.print("B"); //only line changed
}
}
System.out.println("");
}
What is the reason behind the dramatically different run times?
运行时间截然不同的原因是什么?
As suggested in the comments, printing only System.out.print("#");
takes 7.8871
seconds, whereas System.out.print("B");
gives still printing...
.
正如评论所说,仅打印System.out.print("#");
需要7.8871
秒,而System.out.print("B");
给still printing...
。
As others who pointed out that it works for them normally, I tried Ideone.comfor instance, and both pieces of code execute at the same speed.
正如其他人指出它通常对他们有效,例如我尝试了Ideone.com,并且两段代码以相同的速度执行。
Test Conditions:
测试条件:
- I ran this test from Netbeans 7.2, with the output into its console
- I used
System.nanoTime()
for measurements
- 我从Netbeans 7.2运行这个测试,输出到它的控制台
- 我用于
System.nanoTime()
测量
采纳答案by T.J. Crowder
Pure speculationis that you're using a terminal that attempts to do word-wrappingrather than character-wrapping, and treats B
as a word character but #
as a non-word character. So when it reaches the end of a line and searches for a place to break the line, it sees a #
almost immediately and happily breaks there; whereas with the B
, it has to keep searching for longer, and may have more text to wrap (which may be expensive on some terminals, e.g., outputting backspaces, then outputting spaces to overwrite the letters being wrapped).
纯粹的猜测是您正在使用一个尝试进行自动换行而不是字符换行的终端,并将其视为B
单词字符而不#
是非单词字符。因此,当它到达一条线的末尾并寻找可以断线的地方时,它#
几乎立即看到并高兴地在那里断线;而使用B
,它必须继续搜索更长的时间,并且可能有更多的文本要换行(这在某些终端上可能很昂贵,例如,输出退格,然后输出空格以覆盖被换行的字母)。
But that's pure speculation.
但这纯粹是猜测。
回答by Roy Shmuli
I performed tests on Eclipse vs Netbeans 8.0.2, both with Java version 1.8;
I used System.nanoTime()
for measurements.
我在 Eclipse 与 Netbeans 8.0.2 上进行了测试,两者都使用 Java 1.8 版;我是用来System.nanoTime()
测量的。
Eclipse:
蚀:
I got the same time on both cases- around 1.564 seconds.
我在两种情况下都获得了相同的时间- 大约1.564 秒。
Netbeans:
网豆:
- Using "#": 1.536 seconds
- Using "B": 44.164 seconds
- 使用“#”:1.536 秒
- 使用“B”:44.164 秒
So, it looks like Netbeans has bad performance on print to console.
因此,看起来 Netbeans 在打印到控制台时的性能很差。
After more research I realized that the problem is line-wrappingof the max buffer of Netbeans (it's not restricted to System.out.println
command), demonstrated by this code:
经过更多研究,我意识到问题在于Netbeans 的最大缓冲区的换行(不限于System.out.println
命令),如下代码所示:
for (int i = 0; i < 1000; i++) {
long t1 = System.nanoTime();
System.out.print("BBB......BBB"); \<-contain 1000 "B"
long t2 = System.nanoTime();
System.out.println(t2-t1);
System.out.println("");
}
The time results are less then 1 millisecond every iteration except every fifth iteration, when the time result is around 225 millisecond. Something like (in nanoseconds):
每次迭代的时间结果小于 1 毫秒,除了每第五次迭代,当时间结果约为 225 毫秒时。类似于(以纳秒为单位):
BBB...31744
BBB...31744
BBB...31744
BBB...31744
BBB...226365807
BBB...31744
BBB...31744
BBB...31744
BBB...31744
BBB...226365807
.
.
.
And so on..
等等..
Summary:
概括:
- Eclipse works perfectly with "B"
- Netbeans has a line-wrapping problem that can be solved (because the problem does not occur in eclipse)(without adding space after B ("B ")).
- Eclipse 与“B”完美配合
- Netbeans有换行问题可以解决(因为eclipse中不会出现这个问题)(不用在B后加空格(“B”))。
回答by Abdul Alim Shakir
Yes the culprit is definitely word-wrapping. When I tested your two programs, NetBeans IDE 8.2 gave me the following result.
是的,罪魁祸首绝对是自动换行。当我测试您的两个程序时,NetBeans IDE 8.2 给了我以下结果。
- First Matrix: O and # = 6.03 seconds
- Second Matrix: O and B = 50.97 seconds
- 第一个矩阵:O 和 # = 6.03 秒
- 第二个矩阵:O 和 B = 50.97 秒
Looking at your code closely you have used a line break at the end of first loop. But you didn't use any line break in second loop. So you are going to print a word with 1000 characters in the second loop. That causes a word-wrapping problem. If we use a non-word character " " after B, it takes only 5.35 secondsto compile the program. And If we use a line break in the second loop after passing 100 values or 50 values, it takes only 8.56 secondsand 7.05 secondsrespectively.
仔细查看您的代码,您在第一个循环结束时使用了换行符。但是您没有在第二个循环中使用任何换行符。因此,您将在第二个循环中打印一个包含 1000 个字符的单词。这会导致自动换行问题。如果我们在 B 后面使用一个非单词字符“”,编译程序只需要5.35 秒。如果我们在传递 100 个值或 50 个值后在第二个循环中使用换行符,则分别只需要8.56 秒和7.05 秒。
Random r = new Random();
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if(r.nextInt(4) == 0) {
System.out.print("O");
} else {
System.out.print("B");
}
if(j%100==0){ //Adding a line break in second loop
System.out.println();
}
}
System.out.println("");
}
Another advice is that to change settings of NetBeans IDE. First of all, go to NetBeans Toolsand click Options. After that click Editorand go to Formattingtab. Then select Anywherein Line WrapOption. It will take almost 6.24% less time to compile the program.
另一个建议是更改 NetBeans IDE 的设置。首先,转到 NetBeans工具并单击选项。之后单击编辑器并转到格式选项卡。然后选择任何地方的自动换行选项。编译程序所需的时间将减少近 6.24%。