• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

concatenation differance?

 
Tristan Van Poucke
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks for the help guys, much appreciated.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any difference if use StringBuffer instead of StringBuilder?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the StrBuilder class in the Jakarta Common Lang component. It includes a replace method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic