Advertisement
Guest User

Java String Cat Benchmarks

a guest
Jan 8th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.06 KB | None | 0 0
  1. package org.example.benchmarks;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.StringJoiner;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. import org.openjdk.jmh.annotations.Benchmark;
  9. import org.openjdk.jmh.annotations.BenchmarkMode;
  10. import org.openjdk.jmh.annotations.Mode;
  11. import org.openjdk.jmh.annotations.OperationsPerInvocation;
  12. import org.openjdk.jmh.annotations.OutputTimeUnit;
  13. import org.openjdk.jmh.runner.Runner;
  14. import org.openjdk.jmh.runner.RunnerException;
  15. import org.openjdk.jmh.runner.options.Options;
  16. import org.openjdk.jmh.runner.options.OptionsBuilder;
  17.  
  18. import com.google.common.base.Joiner;
  19.  
  20. @OutputTimeUnit(TimeUnit.NANOSECONDS)
  21. @BenchmarkMode(Mode.AverageTime)
  22. @OperationsPerInvocation(MyBenchmark.LIST_SIZE)
  23. public class MyBenchmark {
  24.     private static final char SEPARATOR = '_';
  25.  
  26.     // launch from maven with: clean install exec:exec
  27.     public static void main(String[] args) throws RunnerException {
  28.  
  29.         Options opt = new OptionsBuilder()
  30.                 .include(".*" + MyBenchmark.class.getSimpleName() + ".*")
  31.                 .forks(1).build();
  32.         new Runner(opt).run();
  33.     }
  34.  
  35.     public static final int LIST_SIZE = 20000;
  36.  
  37.     static List<String> sourceList = new ArrayList<>();
  38.     static {
  39.         for (int i = 0; i < LIST_SIZE; i++) {
  40.             sourceList.add("item" + i);
  41.         }
  42.     }
  43.  
  44.     @Benchmark
  45.     public String concatenationUsingPlus() {
  46.         // Considered bad practice since the dawn of Java
  47.         String result = "";
  48.         for (String s : sourceList) {
  49.             if (result.length() > 0) {
  50.                 result += SEPARATOR;
  51.             }
  52.             result += s;
  53.         }
  54.         return result;
  55.     }
  56.  
  57.     @Benchmark
  58.     public String concatenationUsingStringBuffer() {
  59.         // Considered slow because of synchronization
  60.         StringBuffer result = new StringBuffer();
  61.         for (String s : sourceList) {
  62.             if (result.length() > 0) {
  63.                 result.append(SEPARATOR);
  64.             }
  65.             result.append(s);
  66.         }
  67.         return result.toString();
  68.     }
  69.  
  70.     @Benchmark
  71.     public String concatenationUsingStringBuilder() {
  72.         // The approved pre-Java-8 approach
  73.         StringBuilder result = new StringBuilder();
  74.         for (String s : sourceList) {
  75.             if (result.length() > 0) {
  76.                 result.append(SEPARATOR);
  77.             }
  78.             result.append(s);
  79.         }
  80.         return result.toString();
  81.     }
  82.  
  83.     @Benchmark
  84.     public String concatenationUsingGuavaJoiner() {
  85.         // A simple and elegant approach, available pre-Java-8, but with a
  86.         // 3rd-party dependency
  87.         return Joiner.on(SEPARATOR).join(sourceList);
  88.     }
  89.  
  90.     @Benchmark
  91.     public String concatenationUsingStringJoinerOnForLoop() {
  92.         // A Java-8 approach
  93.         StringJoiner result = new StringJoiner("" + SEPARATOR);
  94.         for (String s : sourceList) {
  95.             result.add(s);
  96.         }
  97.         return result.toString();
  98.     }
  99.  
  100.     @Benchmark
  101.     public String concatenationUsingStringJoinerInStreamWithForEachOrdered() {
  102.         // A Java-8 approach
  103.         StringJoiner result = new StringJoiner("" + SEPARATOR);
  104.         sourceList.stream().forEachOrdered(s -> result.add(s));
  105.         return result.toString();
  106.     }
  107.  
  108.     @Benchmark
  109.     public String concatenationUsingStringJoin() {
  110.         // The simplest, most concise Java-8 approach
  111.         return String.join("" + SEPARATOR, sourceList);
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement