为何打印“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
相关文章
相关标签/搜索