Performance of int/long in Python 3

Chris Angelico rosuav at gmail.com
Wed Apr 3 03:24:25 EDT 2013


On Wed, Apr 3, 2013 at 6:06 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Wed, Apr 3, 2013 at 12:52 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> Hmm. I was about to say "Can you just do a quick collections.Counter()
>> of the string widths in 3.3, as an easy way of seeing which ones use
>> BMP or higher characters", but I can't find a simple way to query a
>> string's width. Can't see it as a method of the string object, nor in
>> the string or sys modules. It ought to be easy enough at the C level -
>> just look up the two bits representing 'kind' - but I've not found it
>> exposed to Python. Is there anything?
>
> 4 if max(map(ord, s)) > 0xffff else 2 if max(map(ord, s)) > 0xff else 1

Yeah, that's iterating over the whole string (twice, if it isn't width
4). The system already knows what the size is, I was hoping for an
uber-quick inspection of the string header.

ChrisA



More information about the Python-list mailing list