[Python-Dev] Q: why doesn't list.extend use the sequence interface?

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Sat, 17 Jun 2000 14:45:57 +0200


the eff-bot wrote:

> the first part is trivial; just add something like this to
> Objects/abstract.c:
>=20
> PyObject*
> PySequence_TupleOrList(PyObject* seq)
> ...
>=20
> the second parts is harder -- and that's of course where the
> real performance boost comes from.  as far as I'm aware, very
> few C compilers can "specialize" loops for you...

in the same test setup, moving the PyList_Check inside the
loop gives a 25-30% slowdown (or to look at it from the other
side, 3 times instead of 4 times faster than naive code).

a compromise solution could be to combine the above function
with a macro:

    #define PySequence_TupleOrList_GET_ITEM(seq, index)\
        (PyList_Check(seq) ? PyList_GET_ITEM(seq, index) :\
        PyTuple_GET_ITEM(seq, index)

(anyone got a better name? ;-)

standard usage:

    seq =3D PySequence_TupleOrList(seq_in);

    size =3D PyObject_Length(seq);
   =20
    for (i =3D 0; i < size; i++) {
        PyObject* obj =3D PySequence_TupleOrList_GET_ITEM(seq, i);
        ...
    }

comments?

</F>