Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:30:59+00:00 2026-06-15T14:30:59+00:00

I thought that this is the most efficient way to concatinate strings new StringBuilder(s1.length()

  • 0

I thought that this is the most efficient way to concatinate strings

new StringBuilder(s1.length() + s2.length() + s3.length()).append(s1).append(s2).append(s3).toString();

here StringBuilder’s buffer is created with the capacity enough to fit all strings in it, otherwise StringBuilder may need to expand the buffer up to 3 times.

But when I compile / decompile this

String s4 = s1 + s2 + s3;

I get the actual code in .class built by javac (1.7.0_03) as

String s4 = (new StringBuilder(String.valueOf(s1))).append(s2).append(s3).toString();

Which way is more efficient?

UPDATE

As it was suggested by Steven Schlansker, here is a performance test

                String s1 = "0123456789";
                String s2 = s1 + s1 + s1;
                String s3 = s1 + s1 + s1 + s1 + s1 + s1;
                long t0 = System.currentTimeMillis();
                for (int i = 0; i < 500000; i++) {

                String s4 = new StringBuilder(s1.length() + s2.length() + s3.length()).append(s1).append(s2).append(s3).toString();

//          String s4 = s1 + s2 + s3;

                }
                System.out.println(System.currentTimeMillis() - t0);

it’s not perfect, but the results seem to prove that my version is ~30% faster. On my notebook (Celeron 925) it gives ~230 ms for ver.1 aganist ~300 ms for ver.2. Actually this is what I expected. So I think it would be a good idea if javac compiled string concatination in a more efficient way. There are enough lines like

return "\\Q" + s + "\\E";

even in JDK classes. The last line is from java.util.Pattern which is designed for efficiency

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:31:01+00:00Added an answer on June 15, 2026 at 2:31 pm

    Depends on the environment you execute it in. It’s not possible to conclude definitely that one is better than the other without some sort of test.

    That said, I agree that it’s likely that knowing the final size ahead of time will provide some benefit.

    I suspect that if you write a microbenchmark, you’ll find that the difference is absolutely trivial in almost any application.

    EDIT FOR BENCHMARK ADVICE:

    I see you’ve added some actual data to your post. Congratulations! You’re on the right track 🙂

    Unfortunately, Java has many features which are hostile to writing correct benchmarks. You should probably take a look at How do I write a correct micro-benchmark in Java? if you want your results to be accurate. It’s also worth considering that speedups that look large in this context will look much smaller in the context of a “real” program. A single database fetch (measured in milliseconds) could easily make any gains in memory allocation completely uninteresting (measured in nanoseconds).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to think of the most efficient way to parse a file that
I'm wondering what is the most efficient way of inserting an item (in this
I am looking to find the most efficient way to create an application that
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
I would have thought that this would be an easy thing to do, but
I thought that I improve performance when I replace this code: def f(a, b):
When this quick one hour project came up I never thought that 2 days
After I thought that I've understood how they work, I tried this: NSString *str1
I always thought that *&p = p = &*p in C. I tried this
I came across this code on reddit . I would have thought that type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.