Difference Between Multiple System.out.print () and Concatenation

Basically, I was wondering which approach is better,

for(int i = 0; i < 10000; i++){ System.out.print("blah"); } System.out.println(""); 

or

 String over_9000_blahs = ""; for(int i = 0; i < 10000; i++){ over_9000_blahs += "blah"; } System.out.println(over_9000_blahs); 

or is there an even better way that I don't know about?

+7
java string
source share
5 answers

Since you only write to System.out , the first approach is better BUT , if performance is important to you, use the method below ( System.out.println synchronizes and uses locking - can read more about here and here ).

If you want to use the "big string" later or to improve performance, clear its StringBuilder . (see below), anycase String + will be translated into StringBuilder compiler (more details here )

  StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < 10000; i++){ stringBuilder.append("bla"); } System.out.println(stringBuilder.toString()); 
+4
source share

You want to use StringBuilder if you are concatenating a string in a loop (of a larger number).

 for(int i = 0; i < 10000; i++){ over_9000_blahs += "blah"; } 

What does this mean for each iteration :

  • Creates a new StringBuilder internally with an internal char array large enough to accommodate the intermediate result ( over_9000_blahs )
  • Copy characters from over_9000_blahs to an internal array
  • Copy characters from "blah"
  • Creates a new String , copying characters from the internal array again

So, these are two copies of a longer string per iteration - this means the quadratic complexity of time.


Since System.out.println() can be synchronized , it is likely that its call will repeat more slowly than using StringBuilder (but I assume that it will not be slower than concatenating a string in a loop with += ).

So the StringBuilder approach should be the best of three.

+5
source share

In order of performance:

  • StringBuilder - The fastest. Essentially, it simply adds words to the character array. When capacity is insufficient, it multiplies it. Should occur no more than log (10000) times.

  • System.out.print - It has poor performance compared to StringBuilder , because we need to block out 10,000 times. In addition, printing creates a new char[writeBufferSize] 10,000 times, while in the StringBuilder option we do all this only 1 time!

  • String concatenation. By creating many (and then large ) objects, running some kind of ā€œiā€ memory management will greatly affect performance.

EDIT:

To be more precise, since the question was about the difference between option 2 and option 3 , it is very clear why StringBuilder is fast.

We can say that each iteration in the second approach takes K-time, because the code is the same, and the string length is the same for each iteration. At the end of execution, the second option will take 10,000 * K time for 10,000 iterations. We cannot say the same about the third approach, because the string length always increases for each iteration. Thus, the time for allocating objects and garbage collecting them increases . I am trying to say that the runtime does not increase linearly in the third option. Thus, it is possible that with a low NumberOfIterations we will not see the difference between the last two approaches. But we know that starting with a specific NumberOfIterations second option is always better than the third.

+1
source share

In this case, I would say that the first is better. Java uses StringBuilders to concatenate strings to increase performance, but since Java does not know that you repeatedly perform concatenations using a loop, as in the second case, the first case would be better.

0
source share

If you want only sysout your values ​​- the result will be the same.

The second option will create many lines in memory that the GC (Garbage Collector) takes care of. (But in newer versions of Java, this problem does not occur, because concatenation will be converted to scripts to solve StringBuilder below)

If you want to use your string later, after sysout you should check the StringBuilder and append method:

 StringBuilder sb = new StringBuilder(); for(int i = 0; i < 10000; i++){ sb.append("blah"); } System.out.println(sb); 
0
source share

All Articles