Issue 6688: Optimize PyBytes_FromObject. - Python tracker (original) (raw)
Optimize PyBytes_FromObject by adding special-cases for list and tuple objects and by using _PyObject_LengthHint() instead of an arbitrary value for the size of the initial buffer.
[Without the patch] ./python -m timeit -s "x = list(range(256))" "bytes(x)" 100000 loops, best of 3: 7.19 usec per loop
./python -m timeit -s "x = tuple(range(256))" "bytes(x)" 100000 loops, best of 3: 7.14 usec per loop
./python -m timeit -s "x = list(range(256))*100" "bytes(x)" 1000 loops, best of 3: 591 usec per loop
./python -m timeit -s "x = range(256)" "bytes(x)" 100000 loops, best of 3: 8.45 usec per loop
[With the patch]
./python -m timeit -s "x = list(range(256))" "bytes(x)" 100000 loops, best of 3: 4.43 usec per loop
./python -m timeit -s "x = tuple(range(256))" "bytes(x)" 100000 loops, best of 3: 4.53 usec per loop
./python -m timeit -s "x = list(range(256))*100" "bytes(x)" 1000 loops, best of 3: 335 usec per loop
./python -m timeit -s "x = range(256)" "bytes(x)" 100000 loops, best of 3: 7.56 usec per loop