Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Optimised String Concatenation?

Reply
Thread Tools

Optimised String Concatenation?

 
 
Kamal Advani
Guest
Posts: n/a
 
      03-08-2006
Hello,

Sorry if this topic has been beaten to death, or if there's an FAQ
(kindly point me to it); I'm a bit baffled by the following behaviour:

-----------

public void testTiming() {
long start, stop;

start = System.currentTimeMillis();
for (int i=0; i < 1000; i++) {
getStrBuff(""+new Date()+new Date()+new Date()+new Date());
}
stop = System.currentTimeMillis();
System.out.println("strbuff took (in ms): " + (stop - start));

start = System.currentTimeMillis();
for (int i=0; i < 1000; i++) {
getStrCat(""+new Date()+new Date()+new Date()+new Date());
}
stop = System.currentTimeMillis();
System.out.println("strcat took (in ms): " + (stop - start));
}

private String getStrBuff(String x) {
return new StringBuffer("x is ").append(x).toString();
}

private String getStrCat(String x) {
return "x is " + x;
}

---------

// Output
strbuff took (in ms): 1573
strcat took (in ms): 130

---------

Why does the StringBuffer version run significantly slower (consistent
proportion across multiple runs are obtained)?

I can understand if they're String literals concatenated together, or
Strings whose values are well known in advance, allowing the compiler to
optimise at compile time into a single literal for e.g.; in this case,
dates can only be obtained at runtime - wouldn't that imply that the +
version will create StringBuffers internally?

Thanks.



--
Remove FOOBAR to email
 
Reply With Quote
 
 
 
 
Bart Cremers
Guest
Posts: n/a
 
      03-08-2006
To start, no offense meant, your test sucks.

You're not comparing anything there. You're testing how long it takes
to construct a stringbuffer around a string from the pool against how
long it takes to get the same thing from the pool and concatenate it.
Turn around your test and you'll be even more amazed

A better test would be:

public static void testTiming() {
long start, stop;

start = System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 1000; i++) {
sb.append(new Date());
sb.append(new Date());
sb.append(new Date());
sb.append(new Date());
}

stop = System.currentTimeMillis();
System.out.println("strbuff took (in ms): " + (stop - start));

start = System.currentTimeMillis();
String x = "";
for (int i = 0; i < 1000; i++) {
x += new Date();
x += new Date();
x += new Date();
x += new Date();
}
stop = System.currentTimeMillis();
System.out.println("strcat took (in ms): " + (stop - start));
}

which more or less shows the difference in performance there.

Regards,

Bart

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      03-08-2006
Kamal Advani wrote:

> strbuff took (in ms): 1573
> strcat took (in ms): 130


I suspect that it's nothing to do with the use of StringBuffers, but that you
have run the test for too short a time for the JIT to have fully warmed up.
Run your code inside another loop which calls testTiming():
for (;
{
testTiming();
}
and watch what happens. I'll be interested and surprised if the effect doesn't
vanish.

BTW, what you are timing here will mostly be the time it takes to evaluate:
""+new Date()+new Date()+new Date()+new Date()
since that will take a lot longer than the subsequent call to either
getStrBuffer() or getStrCat().

-- chris


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimised settings for SPA1001? Bonnie Bumble UK VOIP 0 07-30-2006 11:07 PM
wrinting an optimised code junky_fellow@yahoo.co.in C Programming 11 05-19-2005 03:00 PM
How to optimised with embedded Python ? Emmanuel Python 3 02-07-2004 01:02 AM
help: image upload and saving as optimised? mark | r ASP .Net 1 11-07-2003 03:48 PM
ANN: TOGA (Timetables Optimised with Genetic Algorithms) v 0.01 Thuswise Webmaster Python 0 06-28-2003 04:13 PM



Advertisments