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

Java

Avatar of undefined
Last Comment
srikotesh

8/22/2022 - Mon
masheik

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
dpearson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
krakatoa

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
girionis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
krakatoa

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
srikotesh

ASKER
Hi krakatoa,

can you provide the sample example
for the above explanation
krakatoa

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.
srikotesh

ASKER
thanks
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.