aspose file tools*
The moose likes Performance and the fly likes concatenation differance? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "concatenation differance?" Watch "concatenation differance?" New topic
Author

concatenation differance?

Tristan Van Poucke
Ranch Hand

Joined: Jun 30, 2008
Posts: 47
Is there a differance in speed when I use

word.concat(otherword);
OR
word = word + otherword;
OR
word.format("%s",otherword);

?
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

Actually, you want to say:

word = word.concat(otherword)

because concat does not change word, rather it returns a new string. But that is the same as

word = word + otherword

because they both do the same thing: new string whose length is the conbined length of the two original string, copy the contents of the original strings to that new string, and set 'word' to the resulting string.

The

word.format(...)

is completely different - it returns only otherword and does not concatenate with 'word'.


JBoss In Action
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

Oh, and if you really are concerned about performance, and you are building a string in a series of steps (not all in one line), you should use StringBuilder.

If you build the string on a single line, using the '+' operator is sufficient because the compiler generates a StringBuilder for you automatically.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Tristan Van Poucke:
Is there a differance in speed when....


It is highly unlikely that the difference, if any, makes any difference in performance. Unless its inside a loop you do a million times without talking to a database or webserver or user.

This is a classic premature optimization.
Tristan Van Poucke
Ranch Hand

Joined: Jun 30, 2008
Posts: 47
I use it allot, since 90% of the htmlCode in my website is generated, and I always use f.e:
htmlCode = htmlCode + "<table>";
htmlCode = htmlCode + "<tr>;
...

This is too make it more readable for me, but this can't be very performant
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Tristan Van Poucke:
this can't be very performant


Have you measure that its slow? Or are you just speculating?

If a normal application cycle is something like:
1) draw form for user/browser
2) wait for POST
3) do some code
4) talk to DBMS
5) do some formatting
6) go to 1

Then you will quickly find that 99.44% of your time is taken by 2 and 4
and there is no point in optimizing #5

If your current approach is more readable and more maintainable. and you don't *know* that you have a performance problem, ignore it.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

I would suggest changing the HTML-generating code to:

StringBuilder buf = new StringBuilder();
...
buf.append("<table>");
buf.append("<tr>");
...
htmlCode = buf.toString();

This code generates a lot less garbage and does a lot less copying of data from one memory location to the other, and thus will scale better. You might also consider passing a size argument to the StringBuilder constructor.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Peter's suggestion is better style, so when you do new code, use it.

But seriously, this is not important for performance.
Tristan Van Poucke
Ranch Hand

Joined: Jun 30, 2008
Posts: 47
Okay thanks for the help guys, much appreciated.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Peter Johnson:
I would suggest changing the HTML-generating code to:

StringBuilder buf = new StringBuilder();
...
buf.append("<table>");
buf.append("<tr>");
...
htmlCode = buf.toString();

This code generates a lot less garbage and does a lot less copying of data from one memory location to the other, and thus will scale better. You might also consider passing a size argument to the StringBuilder constructor.


Of course this would be totally equivalent to


[ August 27, 2008: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

Yes, Ilja, that would be equivalent if you can write it all as one statement, but I assumed that besides the two simple statements provided that there would be conditions and loops used to build the page. In that case you need more than a single statement and the StringBuilder would be preferred.
[ August 27, 2008: Message edited by: Peter Johnson ]
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Any difference if use StringBuffer instead of StringBuilder?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by sarah Marsh:
Any difference if use StringBuffer instead of StringBuilder?


StringBuffer is thread safe, which theoretically would make StringBuilder a bit faster, where you don't need thread safety.
vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
Any idea why StringBuilder/StringBuffer class does not have replace() like String class?
Would it more efficient to search for text in StringBuilder and replace this text by a new text or to use String.replace() which internally uses regex for string replacement, assume StringBuilder contains 10,000 bytes.
Cheers
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

Check out the StrBuilder class in the Jakarta Common Lang component. It includes a replace method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: concatenation differance?