Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

java - 为什么打印“B”比打印“#”要慢得多?(Why is printing “B” dramatically slower than printing “#”?)

I generated two matrices of 1000 x 1000 :

(我生成了两个1000 x 1000矩阵:)

First Matrix: O and # .

(第一个矩阵: O# 。)
Second Matrix: O and B .

(第二个矩阵: OB)

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:

(测试条件:)

  • 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()进行测量)

  ask by Kuba Spatny translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pure speculation is that you're using a terminal that attempts to do word-wrapping rather than character-wrapping, and treats B as a word character but # as a non-word character.

(纯粹的猜测是,你正在使用一个试图进行自动换行而不是字符换行的终端,并将B视为单词字符,而将#视为非单词字符。)

So when it reaches the end of a line and searches for a place to break the line, it sees a # almost immediately and happily breaks there;

(因此,当它到达一条线的末端并寻找一条断线的地方时,它几乎立刻就会看到#并在那里快乐地打破;)

whereas with the B , it has to keep searching for longer, and may have more text to wrap (which may be expensive on some terminals, eg, outputting backspaces, then outputting spaces to overwrite the letters being wrapped).

(而对于B ,它必须继续搜索更长的时间,并且可能有更多的文本要包装(在某些终端上可能很昂贵,例如,输出退格,然后输出空格来覆盖被包裹的字母)。)

But that's pure speculation.

(但那是纯粹的猜测。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...