[Python-Dev] talking about performance...

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Tue, 20 Jun 2000 20:02:24 +0200


tim wrote:

> No <wink>.  But if someone does, a good compromise might be to split =
this
> into two loops, one for the n=3D=3D1 case and another for n>1.

how about this one:

    if (dir > 0) {
        char *p, *e;
        if (n =3D=3D 0 && i <=3D last)
            return (long)i;
        e =3D s + last - n + 1;
        for (;;) {
            p =3D memchr(s, sub[0], e - s);
            if (p =3D=3D NULL)
                break;
            if (n =3D=3D 1 || memcmp(p, sub, n) =3D=3D 0)
                return (long) (p - s);
            s =3D p + 1;
        }
    }

new record time: 1.6 seconds (down from 2.2)

</F>