This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String concatenation results in CPU load

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I am using string concatenation in a while loop to create a really big string. Could this take the CPU load to 100% or should I look for somethig else for answers to the CPU load problem ?

Thanks,
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes, it could. That's what the CPU is for, after all.

However there's a class called StringBuilder. You should be using that if you're building a string (hence the name) with a non-trivial amount of string concatenation.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrei Antonescu wrote:Hello all,

I am using string concatenation in a while loop to create a really big string. Could this take the CPU load to 100% or should I look for somethig else for answers to the CPU load problem ?

Thanks,



If by "concatenation" you mean either str += someOtherStr or the explicit use of the concatenate() method, then, yeah, that could be a problem for large strings in a tight loop.

However, rather than guessing at where the problem might be, you're better off using a profiler to measure it. JProbe, JProfiler, OptimizeIt, and AppPerfect all have either limited time or limited functionality demo versions, and the JDK comes with jvisualvm.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also concatenate by simple using it like this System.out.print(""i+and so on);
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kurt marfori wrote:You could also concatenate by simple using it like this System.out.print(""i+and so on);


you could, but i believe that is also horribly inefficient.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

kurt marfori wrote:You could also concatenate by simple using it like this System.out.print(""i+and so on);


you could, but i believe that is also horribly inefficient.



I don't know. It's not even clear what he's saying.

If he's just talking about something like


Then it's about as efficient as you can get, in terms of CPU cycles. Of course, since the OP explicitly stated he's doing it in a loop, chances are good that this kind of approach is not possible.

And if that's not what he's talking about, then I have no clue what he means.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could this take the CPU load to 100% or should I look for somethig else for answers to the CPU load problem ?



If it stays at 100% you are looking at an infinite loop instead of a string concat inefficiency. Yes concatenation is quite inefficient and StringBuilders help alleviate that. Depending on what you are trying to do, there could be other ways around concatenation. If you would like to explore other options do let us know what the concatenation attempts to achieve.
 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for posting. I am using StringBuilder.

I have a webapp that generates reports. It takes the report from the database, then uses string concatenation to build a json object that will later send to the client side (javascript).
Sometimes JSON objects are quite big.
A different approach would be to generate an XML directly at the database level and just send it to the client. The CPU doesn't go to 100%, it goes to something like 70% and there are a lot of requests to the webserver.
Can you please indicate me a better way of send data to the javascript side (javascript takes the data via ajax) ?.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic