This week's book giveaways are in the C/C++ and Spring forums.
We're giving away four copies each of C++ Concurrency and Spring Integration in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Performance and the fly likes String concatenation results in CPU load Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of C++ Concurrency this week in the C/C++ forum
or Spring Integration in Action in the Spring forum!
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "String concatenation results in CPU load" Watch "String concatenation results in CPU load" New topic
Author

String concatenation results in CPU load

Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 68
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,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 12983

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.
Jeff Verdegan
Rancher

Joined: Jan 03, 2004
Posts: 1513
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.
kurt marfori
Greenhorn

Joined: Jan 19, 2012
Posts: 12
You could also concatenate by simple using it like this System.out.print(""i+and so on);
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 8139

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.


Never ascribe to malice that which can be adequately explained by stupidity.
Jeff Verdegan
Rancher

Joined: Jan 03, 2004
Posts: 1513
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.

This message was edited 1 time. Last update was at by Jeff Verdegan

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6327

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.


SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 68
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) ?.
 
 
subject: String concatenation results in CPU load
 
Threads others viewed
How to know how many objects are created
Who can help me improve performance of these methods? thanks!
Doubt.......null?
Benefits of using concat()
First Java Program
developer file tools