Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently slicing a string in Python3

Since python does slice-by-copy, slicing strings can be very costly.

I have a recursive algorithm that is operating on strings. Specifically, if a function is passed a string a, the function calls itself on a[1:] of the passed string. The hangup is that the strings are so long, the slice-by-copy mechanism is becoming a very costly way to remove the first character.

Is there a way to get around this, or do I need to rewrite the algorithm entirely?

like image 829
lieblos Avatar asked May 24 '26 09:05

lieblos


1 Answers

The only way to get around this in general is to make your algorithm uses bytes-like types, either Py2 str or Py3 bytes; views of Py2 unicode/Py3 str are not supported. I provided details on how to do this on my answer to a related question, but the short version is, if you can assume bytes-like arguments (or convert to them), wrapping the argument in a memoryview and slicing is a reasonable solution. Once converted to a memoryview, slicing produces new memoryviews with O(1) cost (in both time and memory), rather than the O(n) time/memory cost of text slicing.

like image 64
ShadowRanger Avatar answered May 26 '26 22:05

ShadowRanger