Performance of int/long in Python 3

S

Steven D'Aprano

What it boils down to is:

- it can easily be done by hand now

For some definition of "easily".

if implementation == "CPython":
if version < "3.3":
if sys.maxunicode exists:
use it to decide whether this is a wide or narrow build
if a wide build: return 4
else: return 2
else:
???
elif version == "3.3":
scan the string, in some efficient or inefficient way
return 1, 2, 4 depending on the largest character you find
else:
???
else:
???


- it's a very uncommon need


Well, that at least is true. But then, needing to know the platform
you're running under, the size of objects, the id of a object, the
largest integer, the largest float, or the number of references seen by
the garbage collector are also uncommon needs. What really matters is not
how often you need it, but what you can do when you need it if you don't
have it.
 
I

Ian Kelly

For some definition of "easily".

if implementation == "CPython":
if version < "3.3":
if sys.maxunicode exists:
use it to decide whether this is a wide or narrow build
if a wide build: return 4
else: return 2
else:
???
elif version == "3.3":
scan the string, in some efficient or inefficient way
return 1, 2, 4 depending on the largest character you find
else:
???
else:
???

None of which goes away if a char width function is added to 3.4 and
you still want to support earlier versions as this does. It just adds
another "if".
 
I

Ian Kelly

I really don't see why this means that there can't be a function in
sys, or something. I mean, other Pythons aren't expected to return the
exact same values from sys.getsizeof, are they? But clearly the weight
of opinion is against me, so fine, I don't care that much.

If you want it, nobody is stopping you from writing it yourself as an
extension module. But I don't think the use case is strong enough to
warrant the devs adding it and then having to maintain it.
 
R

Roy Smith

Ian Kelly said:
None of which goes away if a char width function is added to 3.4 and
you still want to support earlier versions as this does. It just adds
another "if".

The same is true of any new feature. That doesn't mean we shouldn't add
new features.
 
I

Ian Kelly

The same is true of any new feature. That doesn't mean we shouldn't add
new features.

If you're interested in backward compatibility, then as noted the
feature doesn't really make things any simpler for you. Otherwise,
the only implementation that matters from the above is the 3.3 one,
which isn't much more complex.
 
S

Serhiy Storchaka

I really don't see why this means that there can't be a function in
sys, or something. I mean, other Pythons aren't expected to return the
exact same values from sys.getsizeof, are they? But clearly the weight
of opinion is against me, so fine, I don't care that much.

The most strong argument for adding this feature in stdlib is that it
has O(1) complexity against of O(N) complexity of any manual
implementation. But this argument is not valid for other implementations.
 
S

Steven D'Aprano

None of which goes away if a char width function is added to 3.4 and you
still want to support earlier versions as this does. It just adds
another "if".

I grant you that for supporting earlier versions. But it will help with
*future* versions. In principle, by Python 3.9, there could be six
different checks just in the CPython section, to say nothing of PyPy,
Jython, IronPython, and any other implementation.

An officially supported way of querying the kind of strings used will
future-proof Python. In this regard, it's no different from (say)
sys.float_info.
 
M

Mark Lawrence

I really don't see why this means that there can't be a function in
sys, or something. I mean, other Pythons aren't expected to return the
exact same values from sys.getsizeof, are they? But clearly the weight
of opinion is against me, so fine, I don't care that much.

ChrisA

There is nothing to stop anybody providing a patch to give this
functionality. The downside is long term someone has to maintain it. I
strongly prefer having python devs spending their time looking after
the 3905 open issues of which 1729 have patches, see
http://comments.gmane.org/gmane.comp.python.devel/138310
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,818
Messages
2,569,732
Members
45,691
Latest member
Dick331194

Latest Threads

Top