StringBuilder insert () vs append () performance?

Is there any performance difference in the insert () vs append () from the StringBuilder class? I will create many short strings as text identifiers and ask myself this question ... Should I initialize SB with a separator and use insert + append or just add?

+4
source share
3 answers

Knowing that:

  • An insertat the end of the string representation is equivalent appendin terms of time complexity (O (n)).
  • And insertsomewhere else, and not at the end, it cannot receive append(since they have different goals).
  • , insert 3 System.arraycopy () , append 1.

:

  • , append
  • insert

, . , , ( ), .

+2

,

:

  • ( , , )
  • , ()

:

  • ( , , )

, , , - .

, append, .

+1

According to Java API docs. You must provide an offset if using an insert.

StringBuilder.insert(5, "String");

but StringBuilder.append("string")no. I guess append has better performance than insert.

-4
source

Source: https://habr.com/ru/post/1617721/


All Articles