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 6736541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:08:29+00:00 2026-05-26T11:08:29+00:00

At first step I run this code: public class Demo { public static void

  • 0

At first step I run this code:

public class Demo  {
    public static void main(String[] args) {
        String x = "x";
        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++)

        {

            x = x.concat("s");

            // x+="k";

        }

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

}

Out: 13579.

On second step I run this code:

public class Demo {
    public static void main(String[] args) {
        String x = "x";
        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++)

        {

            //x = x.concat("s");

             x+="k";

        }

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

}

Out: 27328.

And I have two questions:

  1. Can I say that my banchmark – correct?
  2. Why so big timeline difference between (+) and concat()??? 13.5 sec VS 27 sec. Why?
  • 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-05-26T11:08:29+00:00Added an answer on May 26, 2026 at 11:08 am

    I think that your microbenchmark is fine, and I can reproduce your result.

    On my JVM, the reason x += "k" is twice as slow is that under the covers it does the following:

    1. create a new StringBuilder;
    2. append x to the StringBuilder;
    3. append "k" to the StringBuilder;
    4. call StringBuilder.toString() and assign the result to x.

    This copies the character data twice (once in step 2 and once in step 4).

    On the other hand, x = x.concat("s") only copies the data once.

    This double copying makes x += "k" two times slower than the other version.

    If you’re curious, here are the bytecodes that my compiler has generated for the += loop:

       10:  goto    36
       13:  new #24; //class java/lang/StringBuilder
       16:  dup
       17:  aload_1
       18:  invokestatic    #26; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
       21:  invokespecial   #32; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
       24:  ldc #35; //String k
       26:  invokevirtual   #37; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       29:  invokevirtual   #41; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
       32:  astore_1
       33:  iinc    4, 1
       36:  iload   4
       38:  ldc #45; //int 100000
       40:  if_icmplt   13
    

    Instructions 21 & 29 are where the two copies are made.

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

Sidebar

Related Questions

Here is the code: class Fibonacci { static final int MIN_INDEX = 1; public
This project is the probable first step in migrating a large CMS from Classic
This is my first step into the world of stackoverflow, so apologies if I
trying to run this basic form control example on msdn. At step 1 of
I have in my code the concept of command : public abstract class BaseCommand
Just looking for the first step basic solution here that keeps the honest people
When you are starting a personal programming project, what is your first step? I'm
I was wondering how other developers begin refactoring. What is your first step? How
First off, I understand the reasons why an interface or abstract class (in the
First off, there's a bit of background to this issue available on my blog:

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.