Performance of int/long in Python 3

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Apr 3 03:53:51 EDT 2013


On Wed, 03 Apr 2013 18:24:25 +1100, Chris Angelico wrote:

> 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). 

Then don't write it as a one-liner :-P

n = max(map(ord, s))
4 if n > 0xffff else 2 if n > 0xff else 1


Here's another way:


(sys.getsizeof(s) - sys.getsizeof(''))/len(s)

should work.


There's probably also a way to do it using ctypes.



> The system already knows what the size is, I was hoping for an
> uber-quick inspection of the string header.

I'm not sure that I would want strings to have a method reporting this, 
but it might be nice to have a function in the inspect module to do so. 



-- 
Steven



More information about the Python-list mailing list