Best way to replace tokens in a large text template

后端 未结 10 1784
我在风中等你
我在风中等你 2020-12-08 17:14

I have a large text template which needs tokenized sections replaced by other text. The tokens look something like this: ##USERNAME##. My first instinct is just to use Stri

10条回答
  •  我在风中等你
    2020-12-08 17:36

    FastReplacer implements token replacement in O(n*log(n) + m) time and uses 3x the memory of the original string.

    FastReplacer is good for executing many Replace operations on a large string when performance is important.

    The main idea is to avoid modifying existing text or allocating new memory every time a string is replaced.

    We have designed FastReplacer to help us on a project where we had to generate a large text with a large number of append and replace operations. The first version of the application took 20 seconds to generate the text using StringBuilder. The second improved version that used the String class took 10 seconds. Then we implemented FastReplacer and the duration dropped to 0.1 seconds.

提交回复
热议问题