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'.
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.
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
posted
0
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
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.
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.
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
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 ]
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