[Python-ideas] Exploiting type-homogeneity in list.sort() (again!)
Erik
python at lucidity.plus.com
Tue Mar 7 18:27:00 EST 2017
Hi David,
On 07/03/17 22:39, David Mertz wrote:
> On Tue, Mar 7, 2017 at 4:36 PM, Erik <python at lucidity.plus.com
> <mailto:python at lucidity.plus.com>> wrote:
>
> * Several other methods ('contains', 'remove', 'count', 'index')
> also use PyObject_RichCompareBool(). They could also presumably
> benefit from the same optimisation (perhaps it's not all about
> sort() - perhaps this gives a little more weight to the idea).
>
>
> Good point about list.extend(). I don't think __type_hint__ could help
> with .__contains__() or .count() or .remove(). E.g.:
>
> In [7]: lst = [1.0, 2.0, 1+0j, F(1,1)]
> In [8]: from fractions import Fraction as F
> In [9]: lst = [1.0, 2.0, 1+0j, F(1,1)]
> In [10]: 1 in lst
> Out[10]: True
> In [11]: lst.count(1)
> Out[11]: 3
> In [12]: l.index(1)
> Out[12]: 0
>
>
> The list has absolutely nothing of the right type. Yet it contains an
> item, counts things that are equal, finds a position for an equal item.
Sure, but if the needle doesn't have the same type as the (homogeneous)
haystack, then the rich comparison would still need to be done as a
fallback (and would produce the result you indicate).
But if the needle and the homogeneous haystack have the _same_ type,
then a more optimised version of the operation can be done.
Regards, E.
More information about the Python-ideas
mailing list