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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using trim: trim($dataList, '*'); This will remove all * characters… May 16, 2026 at 6:48 pm
  • Editorial Team
    Editorial Team added an answer Well it depends on whatever use you are going to… May 16, 2026 at 6:48 pm
  • Editorial Team
    Editorial Team added an answer The issue that resulted in this situation was completely unrelated… May 16, 2026 at 6:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have following string: <div> text0 </div> prefix <div> text1 <strong>text2</strong> text3 </div> text4
I found the following C++ code (comments added myself): // frame_name is a char
Why doesn't this produce the correct number of string permutations? For perm(ABC, 3) it
I'm having a problem with model binding. If I do use the prefix in
This has been driving me nuts. I keep getting the following exception System.InvalidOperationException: The
void weight_data::rev_seq(string &seq){ //TODO std::reverse(seq.begin(), seq.end()); } In this C++ method, I think this
I need to fill a String to a certain length with dashes, like: cow-----8
I have a std::vector<std::string> of all the files in a directory: // fileList folder/file1
I have the need to parse partial XML fragments (which are presented as std::string),
I have a fields_for tag, where I specify the prefix (lets say for some

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.