I generated two matrices of 1000 x 1000:

First Matrix: O and #.
Second Matrix: O and B.

Using the following code, the first matrix took 8.52 seconds to complete:

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:

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("#"); takes 7.8871 seconds, whereas System.out.print("B"); gives 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.

Test Conditions:

  • I ran this test from Netbeans 7.2, with the output into its console
  • I used System.nanoTime() for measurements
share|improve this question
48  
Try changing rand.nextInt(4) == 0 to i < 250 to eliminate the effect of the random generator. You might run out of entropy that slows down the random generation – fejese Feb 21 '14 at 23:49
128  
if you are suggesting that printing B takes more time than printing #....why dont you try to print all B & all # rather than relying on random variable r – Kakarot Feb 21 '14 at 23:52
16  
Based on the accepted answer, you apparently didn't try running it with output redirected to a file or /dev/null. – Barmar Feb 23 '14 at 3:21
18  
@fejese, Random() is not a cryptographical rng and so doesn't use up the entropy pool. – Divide Feb 25 '14 at 11:44
8  
@KorayTugay It's a joke. hashes and b-trees are two indexing methods. Hashes usually being faster for equality searches and b-trees for range searches. – slicedtoad Jan 12 '15 at 13:53
up vote 3389 down vote accepted

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. 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, e.g., outputting backspaces, then outputting spaces to overwrite the letters being wrapped).

But that's pure speculation.

share|improve this answer
431  
This is actually the correct answer! Adding a space after the B solves it. – Kuba Spatny Feb 22 '14 at 0:04
198  
There are some answers that come from hard learned experience. T.J. and I (since we are friends) grew up in the days of the Apple ][ and the zx80/81. There was no built in word wrap back then. So we both ended up writing our own — more than once. And those lessons stick with you, they get burned in to your lizard brain. But if you leaned to code after that, when your environment word wraps everything, or you do it manually prior to runtime, it is harder to run into the issues with word wrap. – JockM Feb 23 '14 at 15:42
250  
Brilliant deduction. But we should generalize from this lesson, and always measure performance with output either eliminated, directed to /dev/null (NUL on Windows) or at least to a file. Displaying on any sort of Console is generally very expensive IO, and always distorts timings -- even if not as dramatically confusingly as this. – Bob Kerns Feb 23 '14 at 22:38
27  
@MrLister: System.out.println doesn't do wordwrapping; the thing it was outputting to was doing word-wrapping (and blocking, so System.out.println had to wait). – T.J. Crowder Feb 26 '14 at 22:11
23  
@Chris -- actually, I'll argue that not printing them IS the solution, to the problem of getting accurate timings of the algorithm. Each time you print to a Console (of any sort), you're invoking all manner of external processing not related to what you're testing the performance of. That's a bug in your measurement procedure, pure and simple. On the other hand, if you view the problem not as measurement, but understanding the discrepancy, then yes, not printing is a debugging trick. It comes down to, which problem are you trying to solve? – Bob Kerns Mar 3 '14 at 12:27

I performed tests on Eclipse vs Netbeans 8.0.2, both with Java version 1.8; I used System.nanoTime() for measurements.

Eclipse:

I got the same time on both cases - around 1.564 seconds.

Netbeans:

  • Using "#": 1.536 seconds
  • Using "B": 44.164 seconds

So, it looks like Netbeans has bad performance on print to console.

After more research I realized that the problem is line-wrapping of the max buffer of Netbeans (it's not restricted to System.out.println command), demonstrated by this code:

for (int i = 0; i < 1000; i++) {
    long t1 = System.nanoTime();
    System.out.print("BBB......BBB"); \\<-contain 1000 "B"
    long t2 = System.nanoTime();
    System.out.println(t2-t1);
    System.out.println("");
}

The time results are less then 1 millisecond every iteration except every fifth iteration, when the time result is around 225 millisecond. Something like (in nanoseconds):

BBB...31744
BBB...31744
BBB...31744
BBB...31744
BBB...226365807
BBB...31744
BBB...31744
BBB...31744
BBB...31744
BBB...226365807
.
.
.

And so on..

Summary:

  1. Eclipse works perfectly with "B"
  2. Netbeans has a line-wrapping problem that can be solved (because the problem does not occur in eclipse)(without adding space after B ("B ")).
share|improve this answer
4  
can you elaborate on your research strategies and then what finally led you to find out that line-wrapping was the culprit? (i'm curious about your detective skills, that is!) – silph Oct 30 '16 at 4:49

protected by Robert Levy Feb 25 '14 at 20:05

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.