I generated two matrices of 1000
x 1000
: 我生成了两个1000
x 1000
矩阵: dom
First Matrix: O
and #
. 第一个矩阵: O
和#
。
Second Matrix: O
and B
. 第二个矩阵: O
和B
ide
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秒才能完成: this
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? 运行时间大不相同的缘由是什么? spa
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...
.net
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 ,两段代码都以相同的速度执行。 code
Test Conditions: 测试条件: orm
System.nanoTime()
for measurements 我使用System.nanoTime()
进行测量