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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:27:57+00:00 2026-06-12T13:27:57+00:00

Possible Duplicate: Should I use Java's String.format() if performance is important? I was wondering

  • 0

Possible Duplicate:
Should I use Java's String.format() if performance is important?

I was wondering if is good to use String.format in Java apps instead of StringBuilder… so, I just write a simple test, like this:

public static void main(String[] args) {
        int i = 0;
        Long start = System.currentTimeMillis();
        while (i < 10000) {
            String s = String.format("test %d", i);
            i++;
        }
        System.out.println(System.currentTimeMillis() - start);
        i = 0;
        start = System.currentTimeMillis();
        while (i < 10000) {
            String s = new StringBuilder().append("test ").append(i).toString();
            i++;
        }
        System.out.println(System.currentTimeMillis() - start);
    }

And the results where:

238
15

So, if my test is valid, StringBuilder is faster than String.format. OK.
Now, I start thinking how String.format works. Is it a simple String concatenation, like "test " + i?

What the differences between StringBuilder concatenation and String.format? Is there a way simple as String.format and fast like StringBuilder?

  • 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-12T13:27:58+00:00Added an answer on June 12, 2026 at 1:27 pm

    I wrote a quick caliper benchmark to compare String.format() vs. StringBuilder, StringBuffer, normal String + operator, String.replace() and String.concat() methods:

    public class StringFormatBenchmark extends SimpleBenchmark {
    
        public void timeStringFormat(int reps) {
            while (--reps >= 0) {
                String s = String.format("test %d", reps);
            }
        }
    
        public void timeStringBuilder(int reps) {
            while (--reps >= 0) {
                String s = new StringBuilder("test ").append(reps).toString();
            }
        }
    
        public void timeStringBuffer(int reps) {
            while (--reps >= 0) {
                String s = new StringBuffer("test ").append(reps).toString();
            }
        }
    
        public void timeStringPlusOperator(int reps) {
            while (--reps >= 0) {
                String s = "test " + reps;
            }
        }
    
        public void timeReplace(int reps) {
            while (--reps >= 0) {
                String s = "test {}".replace("{}", String.valueOf(reps));
            }
        }
    
        public void timeStringConcat(int reps) {
            while (--reps >= 0) {
                String s = "test ".concat(String.valueOf(reps));
            }
        }
    
        public static void main(String[] args) {
            new Runner().run(StringFormatBenchmark.class.getName());
        }
    
    }
    

    The results follow (Java 1.6.0_26-b03, Ubuntu, 32 bits):

    caliper2

    Clearly String.format() is much slower (by an order of magnitude). Also StringBuffer is considerably slower than StringBuilder (as we were taught). Finally StringBuilder and String + operator are almost identical since they compile to very similar bytecode. String.concat() is a bit slower.

    Also don’t use String.replace() if simple concatenation is sufficient.

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

Sidebar

Related Questions

Possible Duplicate: When should one use final? When should Java programmers prefer to use
Possible Duplicate: Why should I use templating system in PHP? I was just curious
Possible Duplicate: Why should you use strncpy instead of strcpy? I'm reading a book
Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” When should you use: class MyThread
Possible Duplicate: What Java FTP client library should I use? I am looking for
Possible Duplicate: Understanding Enums in Java Why should we use enums rather Java constants?
Possible Duplicate: When to use intern() When should we use intern method of String?
Possible Duplicate: Java - when to use 'this' keyword Is it generally good convention
Possible Duplicate: Should I use != or <> for not equal in TSQL? Behavior
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there

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.