Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Use "+" in preference to StringBuilder in Java 9 and later  RSS feed

 
Sheriff
Posts: 24089
54
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People here (and all over the Internet) have for years been telling beginner programmers to use a StringBuilder to build a String from small pieces, especially if you're using a loop to do that. But the Java engineers have been beavering away in their back rooms, trying to optimize String concatenation, and now they have come up with a feature whereby it's handled by direct calls to specialized JDK library functions.

You could read for example JDK 9/JEP 280: String Concatenations Will Never Be the Same for details about that.

Or if you want a powerpoint-presentation version with fewer gory details you could read Enough java.lang.String to Hang Ourselves ... (warning: PDF document).

So it would be a good idea, I think, to include this feature in advice about String concatenation in the future.
 
Saloon Keeper
Posts: 2146
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing !! Have a cow.
 
Marshal
Posts: 62881
203
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I tried it using JDK11.0.2. I had a program to test the + operator, and it took about 35″ for 10,000 concatenations, but I was watching for output with a stopwatch. On Java11, that reduced by about 100×, but 1,000,000 append()s to a StringBuilder were still much faster.

Campbell's Computer wrote:java StringConcatenationDemo Hello Campbell
     1 iterations: HelloCampbell
Time =          175ms
    10 iterations: HelloCampbellCampbellCampbellCampbellCampbellCam...
Time =            0ms
   100 iterations: HelloCampbellCampbellCampbellCampbellCampbellCam...
Time =            1ms
 1,000 iterations: HelloCampbellCampbellCampbellCampbellCampbellCam...
Time =           16ms
10,000 iterations: HelloCampbellCampbellCampbellCampbellCampbellCam...
Time =          400ms
100,000 iterations: HelloCampbellCampbellCampbellCampbellCampbellCam...
Time =       35,311ms
With StringBuilder: it took  73ms
Text length = 8,000,005

If you really are daft enough to concatenate Strings in a loop running ≥ 10⁴× you need to test the timings.

[edit]The version of StringConcatenationDemo taking 35″ for 10,000 uses of + had a version number of (0x)36, which I think means Java10; the newer (faster) version which does its own timing has a version number of (0x)37 = Java11. Timings varied by about ±10% in successive runs.
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: I used the StringBuilder the fast way, with a predetermined capacity large enough to hold all my text.
 
Bartender
Posts: 20353
111
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:By the way: I used the StringBuilder the fast way, with a predetermined capacity large enough to hold all my text.



Which is the smart way. StringBuilder defaults to a 16-character work buffer. Add more text, it discards that buffer, moving the data to a buffer that's twice as large. Overflow that, it doubles the buffersize again. And so forth. So a 200-character string would require considerable additional overhead if not pre-allocated.

I may have missed some of the finer details, but it looks like the primary optimisation there is that there's an internal StringBuilder support method that can take all of the elements to be added in parallel and stack them into the buffer in one fell swoop instead of having a separate append call for each added element. That's a good idea, since the overhead for method calls is not trivial, and for short strings and character appends, is likely to be more than the overhead of the actual append logic.
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . Which is the smart way. StringBuilder defaults to a 16-character work buffer. . . . .

Is it 16? I thought it was 64. But whichever way, that is 17/19 copying cycles if you double the array size whenever it is full. I have tried it with a default size of StringBuilder before and that was slower, but still much faster than repeated +. But as I said, the Java11 version is running something like 100× the speed of the older version.

No, I see the StringBuilder documentation says 16. Not sure where I got the 64 from, but that was obviously wrong.
 
Saloon Keeper
Posts: 9710
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure I messed this up some way (benchmarking properly is HARD), because I can't find evidence that String concatenation is faster than using a StringBuilder on Java 11.

[tldr]


The first two entries are the average times of 5 runs, with each run performing an append/concat on a local variable inside a loop 10,000 times.

The second two entries are the mean time of 10,000 one-shot append/concat operations on a field. These two entries must be considered less accurate, because I didn't know how to warm up these tests properly. However, it should be clear that using string concatenation on a field is a bad idea.

[/tldr]

 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This morning, I wrote:By the way: I used the StringBuilder the fast way, with a predetermined capacity large enough to hold all my text.

It didn't make it much slower using the slow way: 73‑98ms instead of about 67‑73ms.
 
Tim Holloway
Bartender
Posts: 20353
111
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to remember also is that there's more than one way to run a JVM. Server mode is supposed to get more aggressive with JIT, as I recall. And the true benchmark would have to be one that didn't contain the overhead of the JIT process itself.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!