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.

Author vstinner
Recipients larry, martin.panter, methane, python-dev, serhiy.storchaka, vstinner
Date 2017-02-02.13:15:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Serhiy Storchaka: "But after converting the struct module to Argument Clinic struct.pack() is faster than int.to_bytes() again!"

Sorry about that ;-)

Serhiy Storchaka: "Now I need to find other ways to make int.to_bytes() even faster to win this chase."

I ran a microbenchmark:

$ ./python -m perf timeit -s 'to_bytes=int.to_bytes' 'to_bytes(1, 4, "little")'

Reference: ~154 ns

Replace int_to_bytes_impl() body with:
   PyBytes_FromStringAndSize("1", 1)
=> ~120 ns (-34 ns)

Replace int_to_bytes() body with:
   return int_to_bytes_impl(self, 4, NULL, 1);
=> ~76 ns (-44 ns)

_PyArg_ParseStackAndKeywords() with _PyArg_Parser{"nU|$p:to_bytes"} takes 44 ns on a total of 154 ns. 29% of the runtime is spent on parsing arguments.

If you want to optimize further int.to_bytes(), IMHO we should explore the issue #29419: "Argument Clinic: inline PyArg_UnpackTuple and PyArg_ParseStack(AndKeyword)?".
Date User Action Args
2017-02-02 13:15:48vstinnersetrecipients: + vstinner, larry, methane, python-dev, martin.panter, serhiy.storchaka
2017-02-02 13:15:48vstinnersetmessageid: <>
2017-02-02 13:15:48vstinnerlinkissue29300 messages
2017-02-02 13:15:48vstinnercreate