This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Optimize PyBytes_FromObject.
Type: performance Stage: resolved
Components: Interpreter Core Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: alexandre.vassalotti, amaury.forgeotdarc
Priority: normal Keywords: patch

Created on 2009-08-11 21:34 by alexandre.vassalotti, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
optimize_bytes_from_object.diff alexandre.vassalotti, 2009-08-11 21:34
Messages (3)
msg91486 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2009-08-11 21:34
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
msg91861 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-08-22 12:02
Nice improvement!
Beware that _PyObject_LengthHint may set an exception, though.
msg97474 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2010-01-09 22:15
Committed in r77398.
History
Date User Action Args
2022-04-11 14:56:51adminsetgithub: 50937
2010-01-09 22:15:17alexandre.vassalottisetstatus: open -> closed
resolution: accepted
messages: + msg97474

stage: patch review -> resolved
2009-08-22 12:02:10amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg91861
2009-08-11 21:34:36alexandre.vassalotticreate