Slow string concatenation over large input

前端 未结 6 777
醉酒成梦
醉酒成梦 2021-01-12 13:47

I\'ve written an n-ary tree ADT which works fine. However, I need to store its serialization in a variable a calling class. eg.

    DomTree a         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 14:26

    If a profiler confirms you that the bottleneck is string concatenation you have two choices:

    • StringBuilder/StringBuffer (the latter is better suited for threading)
    • Ropes for Java:

    A rope is a high performance replacement for Strings. The datastructure, described in detail in "Ropes: an Alternative to Strings", provides asymptotically better performance than both String and StringBuffer for common string modifications like prepend, append, delete, and insert. Like Strings, ropes are immutable and therefore well-suited for use in multi-threaded programming.

提交回复
热议问题