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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:14:00+00:00 2026-06-10T04:14:00+00:00

I am trying some performance benchmark regarding String Pool. However, the outcome is not

  • 0

I am trying some performance benchmark regarding String Pool. However, the outcome is not expected.

I made 3 static methods

  • perform0() method … creates a new object every time
  • perform1() method … String literal “Test”
  • perform2() method … String constant expression “Te”+”st”

My expectation was (1. fastest -> 3. slowest)

  1. “Test” because of string pooling
  2. “Te”+”st” because of string pooling but bit slower than 1 because of + operator
  3. new String(..) because of no string pooling.

But the benchmark shows that “Te”+”st” is slighty faster than “Test”.

new String(): 141677000 ns 
"Test"      : 1148000 ns 
"Te"+"st"   : 1059000 ns

new String(): 141253000 ns
"Test"      : 1177000 ns
"Te"+"st"   : 1089000 ns

new String(): 142307000 ns
"Test"      : 1878000 ns
"Te"+"st"   : 1082000 ns

new String(): 142127000 ns
"Test"      : 1155000 ns
"Te"+"st"   : 1078000 ns
...

Here’s the code:

import java.util.concurrent.TimeUnit;


public class StringPoolPerformance {

    public static long perform0() {
        long start = System.nanoTime();
        for (int i=0; i<1000000; i++) {
            String str = new String("Test");
        }
        return System.nanoTime()-start;
    }

    public static long perform1() {
        long start = System.nanoTime();
        for (int i=0; i<1000000; i++) {
            String str = "Test";
        }
        return System.nanoTime()-start;
    }

    public static long perform2() {
        long start = System.nanoTime();
        for (int i=0; i<1000000; i++) {
            String str = "Te"+"st";
        }
        return System.nanoTime()-start;
    }

    public static void main(String[] args) {
        long time0=0, time1=0, time2=0;
        for (int i=0; i<100; i++) {
            // result
            time0 += perform0();
            time1 += perform1();
            time2 += perform2();
        }

        System.out.println("new String(): " +  time0 + " ns");
        System.out.println("\"Test\"      : " + time1 + " ns");
        System.out.println("\"Te\"+\"st\"   : " + time2 + " ns");
    }
}

Can someone explain why “Te”+”st” performs faster than “Test”? Is JVM doing some optimizations here? Thank you.

  • 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-10T04:14:02+00:00Added an answer on June 10, 2026 at 4:14 am

    "Te" + "st" is a compiler-time constant expression, and so will behave at runtime no differently than simply "Test". Any performance hit will be when trying to compile it, not when trying to run it.

    That’s easily proven by disassembling your compiled benchmark class using javap -c StringPoolPerformance:

    public static long perform1();
      Code:
    ...
       7:   ldc #3; //int 1000000
       9:   if_icmpge   21
       12:  ldc #5; //String Test
       14:  astore_3
       15:  iinc    2, 1
    ...
    
    public static long perform2();
      Code:
    ...
       7:   ldc #3; //int 1000000
       9:   if_icmpge   21
       12:  ldc #5; //String Test
       14:  astore_3
       15:  iinc    2, 1
    ...
    

    The methods’ byte code are absolutely identical! This is specified by the Java Language Specification, 15.18.1:

    The String object is newly created (§12.5) unless the expression is a compile-time constant expression (§15.28).

    The benchmark difference you experience is probably due to typical variability or because your benchmark isn’t perfect. See this question: How do I write a correct micro-benchmark in Java?

    Some notable rules you break:

    1. You don’t discard the results of the “warm-up” iterations of your test kernel.
    2. You don’t have GC logging enabled (particularly relevant when perform1() is always being run right after the test which creates a million objects).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am trying to squeeze some performance out of this query, any help would be
I'm trying to use SQL Server Profiler (2005) to track down some application performance
I am trying to work out some performance problems with some JavaScript I've been
I've been trying to debug some super slow performance in running my cucumber features.
Not sure if this question makes for some poor performance down the track, but
I'm trying to add some performance counters to my asp.net website. Now, I know
I am trying to get some detailed performance information from an application my company
I'm trying to track down some performance issues I have had with Django. There
We're trying to find out some performance fixes reading from a TADOQuery. Currently, we
I have been trying to perform some OpenGL ES performance optimizations in an attempt

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.