Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

String concatenation and Stringbuider which one is the faster.

Hi Experts,

String concatenation and Stringbuider which one is the faster.
what is the better appraoch to use while framing queries.

String concatenation will create one object or multiple?(I guess it will create one object).

example:

String concatenation:
=================
String query = select * from testsample where testsample_id='"+id+"'
                          +" and name='"+name+"'
                          +" and deleteflag ='"+N+"'

StringBuilder :
===========
StringBuilder sb = new StringBuilder();
sb.append("select * from testsample where testsample_id='"+id+"'");
   .append("and name='"+name+'"");
   .append("and deleteflag ='"+N+'"");

Open in new window

Avatar of masheik
masheik
Flag of India image

I think using ORM is somewhat slower than direct query execution since , it has it is own construction and logic behind to do so along with your piece of code.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In terms of raw speed, you'd find that the variance between using concatenation or adding to a StringBuilder narrows significantly, and is effectively asymptotic. But you'd have to be dealing with a lot of strings, but it's worth bearing in mind.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpearson
dpearson

For a little historical context on this - in the early versions of Java, there was "StringBuffer".
So people wrote code using StringBuffer to create simple Strings.

But then StringBuilder was added in Java 1.5.  StringBuilder is faster than StringBuffer (no thread synchronization).
At that point, if you had code that was written using simple String concatenation, the compiler switched to using StringBuilder and your code got faster with no work on your part.  But if you wrote the code explicitly using StringBuffer, the compiler had to continue to use StringBuffer, so your code didn't get faster.

Hence why today we should use String concatenation for assembling Strings in a single line - it allows the compiler to "do what's best" and "what's best" can change over time as the compiler gets more optimized.

Doug
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srikotesh

ASKER

Hi krakatoa,

can you provide the sample example
for the above explanation
Well, I can't find the code I actually used, as I don't hang on to snippets for long, but you'd just need to declare a String and a StringBuilder, turn your List into an Array, and loop over an n-sized source of Strings for the two scenarios. I used the same source Strings for the concatenation and the StringBuilder, and between the loops, decluttered memory using System.gc() between the two scenario loops. Free memory can be obtained from

Runtime.getRuntime().freeMemory();

But as you can see from the results I posted already, there's a noticeable a difference in the free memory figures, but not a difference in terms of order of magnitude, so I can't see how you are going to be affected by much speed or memory concerns, unless you are dealing with monster numbers of Strings. Others may disagree.
thanks