爲何打印「B」比打印「#」要慢得多? - Why is printing 「B」 dramatically slower than printing 「#」?

問題:

I generated two matrices of 1000 x 1000 : 我生成了兩個1000 x 1000矩陣: dom

First Matrix: O and # . 第一個矩陣: O#
Second Matrix: O and B . 第二個矩陣: OB 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

  • I ran this test from Netbeans 7.2 , with the output into its console 我從Netbeans 7.2運行了這個測試,輸出到它的控制檯
  • I used System.nanoTime() for measurements 我使用System.nanoTime()進行測量

解決方案:

參考一: https://stackoom.com/question/1U5XA/爲何打印-B-比打印-要慢得多
參考二: https://oldbug.net/q/1U5XA/Why-is-printing-B-dramatically-slower-than-printing
相關文章
相關標籤/搜索