On Tue, Dec 23, 2014 at 7:46 AM, Otávio Gonçalves de Santana <
otavioj...@java.net> wrote:

>
> *Motivation:* StringBuffer is synchronized while StringBuilder is not
> which makes StringBuilder faster than StringBuffer[1]. The strategy was
> removed the StringBuffer when it is not necessary.
>
>
> WebRev:
> https://dl.dropboxusercontent.com/u/16109193/open_jdk/jaxp/buffer2builder/index.html
>
>
> [1]
>
> @State(Scope.Thread)
> @OutputTimeUnit(TimeUnit.SECONDS)
> public class StringBufferBenchmark {
>
>
>     private List<String> texts;
>     @Param("10000000")
>     private int param;
>
>     @Setup
>     public void setup() {
>         texts = new LinkedList<>();
>         Random random = new Random();
>         for (int index = 0; index < param; index++) {
>             char a = (char) random.nextInt();
>             texts.add(String.valueOf(a));
>         }
>     }
>
>     @GenerateMicroBenchmark
>     public void stringBuffer(BlackHole bh) {
>         StringBuffer sb = new StringBuffer();
>         texts.stream().forEach(sb::append);
>         bh.consume(sb.toString());
>     }
>
>     @GenerateMicroBenchmark
>     public void stringBuilder(BlackHole bh) {
>         StringBuilder sb = new StringBuilder();
>         texts.stream().forEach(sb::append);
>         bh.consume(sb.toString());
>     }
>  }
>
>
> java -jar target/microbenchmarks.jar ".*StringBufferBenchmark*." -wi 20 -i
> 20 -f 1
>
> Benchmark                                (param)   Mode   Samples
> Mean   Mean error    Units
> m.StringBufferBenchmark.stringBuffer    10000000  thrpt        20
>  3.208        0.406    ops/s
> m.StringBufferBenchmark.stringBuilder   10000000  thrpt        20
>  4.795        0.037    ops/s
> --
>
> Otávio Gonçalves de Santana
>
> twitter: http://twitter.com/otaviojava
> site:     *http://about.me/otaviojava <http://about.me/otaviojava>*
> 55 (11) 98255-3513
>



-- 
Otávio Gonçalves de Santana

blog:     http://otaviosantana.blogspot.com.br/
twitter: http://twitter.com/otaviojava
site:     *http://about.me/otaviojava <http://about.me/otaviojava>*
55 (11) 98255-3513

Reply via email to