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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:41:16+00:00 2026-05-16T05:41:16+00:00

String prefix = ; for (String serverId : serverIds) { sb.append(prefix); prefix = ,;

  • 0
String prefix = "";
for (String serverId : serverIds) {
  sb.append(prefix);
  prefix = ",";
  sb.append(serverId);
}

The following code runs faster than the above code . the “,” prefix object does unnecessary object creation on every iteration . The above code takes 86324 nano seconds,while mine takes only 68165 nano seconds.

List<String> l =  Arrays.asList("SURESH1","SURESH2","SURESH4","SURESH5");
StringBuffer  l1 = new StringBuffer();
int sz = l.size(); 
int i=0; long t =
System.nanoTime();
for (String s : l)
{ 
   l1.append(s);     
   if  ( i != sz-1)
        l1.append(",");   i++;
   } 
} 
long t2 = System.nanoTime();
System.out.println ((t2-t)); System.out.println(l1);

// The time taken for the above code is 68165 nano seconds
SURESH1,SURESH2,SURESH4,SURESH5

kindly let me know which one is better in ur view.

  • 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-16T05:41:16+00:00Added an answer on May 16, 2026 at 5:41 am

    A few points:

    • My code doesn’t require you to know the number of elements up-front. In other words, it can work over any Iterable<String>
    • Why are you using StringBuffer at all rather than StringBuilder?
    • The “empty prefix object” is only created once… how sure are you that there aren’t any references to an empty string literal anywhere in your code?
    • Which code do you find simpler to read? That’s likely to be more important than timing in most cases. (Currently your code as posted appears not to have enough open braces, for example…)
    • Why not use a library method in the first place (e.g. Guava’s Joiner class)?
    • Never use timings this small in a benchmark. How accurate do you expect your system clock to be? You should repeat the same operation many, many until it’s taken a sensible amount of time.

    EDIT: Now one alternative which addresses the first point would be this change:

    boolean first = true;
    StringBuilder builder = new StringBuilder();
    for (String value : values) {
      if (first) {
        first = false;
      } else {
        builder.append(",");
      }
      builder.append(value);
    }
    

    Or if you really like using a counter:

    int i = 0;
    StringBuilder builder = new StringBuilder();
    for (String value : values) {
      if (i != 0) {
        builder.append(",");
      }
      builder.append(value);
      i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code, void Generate(List<string> comb, string prefix, string remaining) { int currentDigit
I have a the following generic methods: public static string Glue(string prefix, string value)
Edit: I've reduced a majority of the errors with the std:: prefix on string
Is including a application package prefix while defining a custom action string is convention
In Python, I can prefix an r to a string literal (raw string) to
string name = bob; object obj = name; obj = joe; Console.WriteLine(name); I'm a
startsWith(string prefix) requires two strings and searches if both strings start with the same
I found the following C++ code (comments added myself): // frame_name is a char
How can I remove a sub-string (a prefix) from a array of string elements?
I'm using some Java code to do fast prefix lookups, using java.util.TreeSet, could I

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.