[issue40313] bytes.hex(sep, bytes_per_sep) is many times slower than manually inserting the separators

Dennis Sweeney report at bugs.python.org
Sun Apr 19 03:37:59 EDT 2020


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

I replicated this behavior. This looks like the relevant loop in pystrhex.c:

    for (i=j=0; i < arglen; ++i) {
        assert((j + 1) < resultlen);
        unsigned char c;
        c = (argbuf[i] >> 4) & 0x0f;
        retbuf[j++] = Py_hexdigits[c];
        c = argbuf[i] & 0x0f;
        retbuf[j++] = Py_hexdigits[c];
        if (bytes_per_sep_group && i < arglen - 1) {
            Py_ssize_t anchor;
            anchor = (bytes_per_sep_group > 0) ? (arglen - 1 - i) : (i + 1);
            if (anchor % abs_bytes_per_sep == 0) {
                retbuf[j++] = sep_char;
            }
        }
    }

It looks like this can be refactored a bit for a tighter inner loop with fewer if-tests. I can work on a PR.

----------
nosy: +Dennis Sweeney
versions: +Python 3.9 -Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40313>
_______________________________________


More information about the Python-bugs-list mailing list