How to replace the charactors in the string?

Perl uses reference counting for memory management, and it is probably more efficient for allocating and then discarding many small strings like you are doing here. (Indeed, working with lots of small strings is probably the main situation that Perl’s memory management is optimized for.) (Julia uses mark-and-sweep garbage collection, because reference counting imposes other tradeoffs for compiled and multithreaded code.)

The real question here: in what circumstance is the tr operation on small strings performance-critical? In a given realistic setting, there is usually a way to avoid doing lots of re-allocations in your inner loop…

2 Likes