I generated two matrices of 1000
x 1000
:
(我生成了两个1000
x 1000
矩阵:)
First Matrix: O
and #
.
(第一个矩阵: O
和#
。)
Second Matrix: O
and B
.
(第二个矩阵: 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("#");
(正如评论中所建议的那样,只打印System.out.print("#");
)
takes 7.8871
seconds, whereas System.out.print("B");
(需要7.8871
秒,而System.out.print("B");
)
gives still printing...
. (still printing...
)
As others who pointed out that it works for them normally, I tried Ideone.com for instance, and both pieces of code execute at the same speed.
(正如其他人指出它通常适用于他们一样,我尝试过Ideone.com ,两段代码都以相同的速度执行。)
Test Conditions:
(测试条件:)
ask by Kuba Spatny translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…