views:

102

answers:

2
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.

+2  A: 

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++;
}
Jon Skeet
@jon thanks for the tips. sorry "," is not created on every iteration , it refers to the same string pool.
Suresh S
@jon skeet i repeated this with 80,000 elements still my code does faster.
Suresh S
@Suresh S: Even with 80,000 values I'd still be suspect... how long does it take in absolute terms? I'd take any result of less than a second with a pinch of salt. There are various other things which could go wrong with benchmarking... we'd have to see your full code to say for sure.
Jon Skeet
Suresh S
A: 

I also have serious doubts about the way you have coded and run your benchmarks. For a start, your timings suggest that your code didn't get JIT compiled. There are many mistakes that people make with Java benchmarks that can invalidate the results. Show us the complete code.

The other point is that in most cases, this kind of micro-optimization is irrelevant to the performance of real programs. Either the program already runs fast enough, or you are wasting your time optimizing the wrong part of the program.

Stephen C