In C++ there's vector::reserve() to efficiently allocate memory in advance when building a string. Even without it we can build a string in O(n) amortized.
How do we efficiently build strings in languages where strings are immutable, such as python? The naive method of adding an atom at a time, that works fine in C++ in O(n), seems to be O(n^2), generating O(n^2) garbage for the gc.
Aucun commentaire:
Enregistrer un commentaire