Performance and simplicity tradeoffs between String, StringBuffer, and StringBuilder

后端 未结 5 845
灰色年华
灰色年华 2021-02-09 17:29

Have you ever thought about the implications of this change in the Java Programming Language?

The String class was conceived as an immutable class (and this decision was

5条回答
  •  离开以前
    2021-02-09 17:57

    I tried the same thing on an XP machine. the StringBuilder IS somewhat faster but if You reverse the order of the run, or make several runs You'll notice that the "almost factor two" in the results will be changed into something like 10% advantage:

    StringBuffer build & output duration= 4282,000000 µs
    StringBuilder build & output duration= 4226,000000 µs
    StringBuffer build & output duration= 4439,000000 µs
    StringBuilder build & output duration= 3961,000000 µs
    StringBuffer build & output duration= 4801,000000 µs
    StringBuilder build & output duration= 4210,000000 µs
    

    For Your kind of test the JVM will NOT help out. I had to limit the number of runs and elements just to get ANY result from a "String only"-test.

提交回复
热议问题