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 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:
System.nanoTime()
for measurementsPure 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.
Answered 2023-09-20 20:22:33
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). - anyone I performed tests on Eclipse vs Netbeans 8.0.2, both with Java version 1.8;
I used System.nanoTime()
for measurements.
I got the same time on both cases - around 1.564 seconds.
So, it looks like Netbeans has a 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"); // <- contains 1000 "B"s
long t2 = System.nanoTime();
System.out.println(t2 - t1);
System.out.println("");
}
The time results are less than 1 millisecond every iteration except every fifth iteration, when the time result is around 225 milliseconds. 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.
Answered 2023-09-20 20:22:33
Yes, the culprit is definitely word-wrapping. When I tested your two programs, NetBeans IDE 8.2 gave me the following result.
Looking at your code closely: you have used a line break at the end of the first loop. But you didn't use any line breaks in the second loop. So you are going to print a word with 1000 characters in the second loop. That causes a word-wrapping problem. If we use a non-word character " " after B, it takes only 5.35 seconds to compile the program. And If we use a line break in the second loop after passing 100 values or 50 values, it takes only 8.56 seconds and 7.05 seconds respectively.
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");
}
if (j % 100 == 0) { // Adding a line break in second loop
System.out.println();
}
}
System.out.println("");
}
Another advice is to change the settings of NetBeans IDE. First of all, go to NetBeans Tools and click Options. After that, click Editor and go to the Formatting tab. Then select Anywhere in Line Wrap Option. It will take almost 6.24% less time to compile the program.
Answered 2023-09-20 20:22:33