Does it make any difference in the above examples if any/all of the strings is/are string literal(s)?
TIA, [ August 18, 2005: Message edited by: Tom Blough ]
Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Norm Radder
Ranch Hand
Joined: Aug 10, 2005
Posts: 681
posted
0
Write a test program with a loop to do your code thousands of times and time it. Run the test hundreds of times to smooth out any system interruptions. Analyze the results and Post the results here. Then we'll all know.
Ernest Friedman-Hill
author and iconoclast
Marshal
It does make a difference if they're all literals; if they are, the "+" can be done by the compiler, so at runtime there's just one String to append; that makes the + version a bit more efficient.
If they're not then the three-append version is better; the +'s will be compiled into the creation of another StringBuffer and some more append calls.
I don't really recommend trying to micro-benchmark this yourself, for the simple reason that any differences will be so small they'll be hard to measure. This really doesn't make a whole lot of difference in most cases.
That's kinda what I though, but I didn't know if there was a clear winner. To me, both versions seem just as clear from a readability standpoint, so I guess I'll continue to use the concatination version as there is less typing required.
Norm, if I had time, I'd code it an test it, but nobody is paying for that, so it will have to wait.
Cheers,
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Ernest actually meant "constant", not "literal", though you'll find a significant performance degradation (I've see an order of magnitude of about 15000) if you append non-constants within a loop. The million dollar question is why? I'll leave you to ponder.
In any case, design first, optimise later - as long as you have a robust regression, this is a trivial implementation issue compared to many other potential issues - spend 95% of your effort on ensuring the integrity of your contracts, since this is the part that cannot change. The biggest mistake I see is not clarifying the distinction between contract and implementation - for example, even as much as a non-private constructor is enough to expose unnecessary implementation detail.
If the Strings aren't compile time constants, this is equivalent to
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
If the Strings aren't compile time constants, this is equivalent to [/qb] I don't think so. String1+String2+String3 is evaluated first, which is creating implizit new Strings first (String1+String2), ((String1+String2)+String3), before StringBuffer.append is called for that generated new String. [ August 19, 2005: Message edited by: Stefan Wagner ]
The benchmark suggests that the form 'false' (2x +) needs 1.5 to 2-times the time compared to 'true' (3x append). And the second suggestion: You get about 5 million calls per second on my machine.
It's not science-solid-measurement, but my experience shows, that I can rely on the results, if the amount of time is greater than a second, and the differences are greater than 20%. [ August 19, 2005: Message edited by: Stefan Wagner ]
Rick O'Shay
Ranch Hand
Joined: Sep 19, 2004
Posts: 531
posted
0
Side note: use StringBuilder in 5.0 and serialize access to the object when the need arises. Since 5.0 I use StingBuffer precisely never, 'least not to new code.
Ronnie Ho
Ranch Hand
Joined: Aug 10, 2005
Posts: 47
posted
0
Originally posted by Stefan Wagner:
If the Strings aren't compile time constants, this is equivalent to
I don't think so. String1+String2+String3 is evaluated first, which is creating implizit new Strings first (String1+String2), ((String1+String2)+String3), before StringBuffer.append is called for that generated new String.
[ August 19, 2005: Message edited by: Stefan Wagner ][/QB]
I think the compiler automatically translates the + operator into StringBuffer.append() even before the code is executed by the JVM execution engine. So, the compiler indeed interprets
AS
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Ronnie Ho: I think the compiler automatically translates the + operator into StringBuffer.append() even before the code is executed by the JVM execution engine. So, the compiler indeed interprets
AS [/QB]
According to the JLS 15.18.1.2, the compiler *is allowed* to do so. As far as I know, all current compilers do.
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
the 1.5 compiler doesn't
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Tony Morris: the 1.5 compiler doesn't
Good catch. It uses StringBuilder (?) instead, doesn't it? Of course that doesn't change the argument...
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Well, since I work on *the* 1.5 implementation as my regular job, and it hasn't been released yet, I'm not at liberty to say But if you're referring to the Sun implementation, last I looked, it uses java.lang.StringBuilder; yes, your point still stands.