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()
進行測量