3 ms·
I'd be concerned about `value[0]` — how does that work in the face of multi byte characters? Is all string indexing in Python O(n)? Does it store whether a give
by bobbylarrybobby 2y ago
I'd be concerned about `value[0]` — how does that work in the face of multi byte characters? Is all string indexing in Python O(n)? Does it store whether a given string is ascii-only and switch to constant time lookup if it is?
- Sniffnoy 2y agoPython 3 actually uses UTF-32, so it's all constant-time. A tradeoff few make, certainly!
- lifthrasiir 2y agoOr more accurately, behaves as if it is UTF-32. The actual implementation uses multiple internal representations, just like JS engines emulating UCS-2.
- Sniffnoy 2y agoHuh! I was unaware (of both of those), thanks.
- jerf 2y agoThe cost of string indexing isn't relevant for a hard-coded zero index. It affects what you might get back but that's O(1) regardless of implementation.