Java String Concatenation Benchmark

download Java String Concatenation Benchmark

of 3

Transcript of Java String Concatenation Benchmark

  • 8/13/2019 Java String Concatenation Benchmark

    1/3

    String

    ConcatenationPerformanceBenchmark

    CONTENTS INCLUDE:. String Concatenation Operation. Performance Comparison. Summary. Source Code

    Java String

    Java String is the most used immutablString operations are using create-ne

    String str = hello;str = str.toUppercase();

    This snippet of code does not modifywith all the characters of str is changvariable refers to it.

    String Concatenation

    String concatenation operation can b Concatenation operation + StringBuffer.append() StringBuilder.append()

    Concatenation operation + to JavConcatenation operation using StrinSystem.arraycopy() - the new string

    StringBuilder is compatible API with

    Charts below are the performance cstring in a loop. This benchmark is dX-axis is the number of loops; Y-axis

    A v e r a g e e

    l a p s e

    d t i m e

    i n n a n o s e c o n

    d s

    STRING CONCATENATION OPER

    PERFORMANCE COMPARISON

    String Concatenati

    le class in Java. Immutable means cant be changw and throw-old strategy.

    value of object that variable str refers to. Instead to upper case. The old hello string will be ga

    e achieved using,to strings

    Strings is done by creating new string and the oBuffer.append and StringBuilder.append is doneo an array of chars containing the old string.

    tringBuffer but with no guarantee of synchroniza

    omparison of mentioned concatenation operation one using Java 6 Hotspot with JIT enabled.

    is the average elapsed time per operation.

    Number of loops

    TION

    REFERENCE CARDon Benchmark

    By Leonar Tambunan

    d or mutated. All of the Java

    , a new String object is createdrbage collected since no more

    ld string thrown away.by appends - using native code

    tion.

    above by appending abcde

  • 8/13/2019 Java String Concatenation Benchmark

    2/3StringConcatenationPerformanceBenchmark

    Performance Comparison continued

    1. For small number of iterationdoesnt really significant desiterations, StringBuffer & Stri

    2. There is no significant differeStringBuffer.

    3. The champion of string conca

    ABOUT THE AUTHOR

    Leonar Tambunan is a software [email protected]

    A v e r a g e e

    l a p s e

    d t i m e

    i n n a n o s e c o n

    d s

    A v e r a g e e

    l a p s e

    d t i m e

    i n n a n o s e c o n

    d s

    SUMMARY

    Reference Card

    s (

  • 8/13/2019 Java String Concatenation Benchmark

    3/3