-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize bytearray % args #69585
Comments
Optimize bytearray % args Don't create temporary bytes objects: modify _PyBytes_Format() to create work
|
Microbenchmark result below. Most operations are now between 2.5 and 5 times faster. %f is as-fast, probably because formatting a float is more expensive than copying bytes (raw estimation: 150 ns to format a single floating pointer number). Common platform: Platform of campaign orig: Platform of campaign no_copy: -------------------------------------------------+-------------+-------------- fmt = bytearray(b"hello %s"); fmt % b"world" | 656 ns (*) | 93 ns (-86%)
fmt = bytearray(b"hello %-100s"); fmt % b"world" | 686 ns (*) | 105 ns (-85%)
fmt = bytearray(b"x=%d"); fmt % 123 | 689 ns (*) | 112 ns (-84%)
fmt = bytearray(b"x=%f"); fmt % 1.2 | 976 ns (*) | 216 ns (-78%)
fmt = bytearray(b"x=%100d"); fmt % 123 | 870 ns (*) | 172 ns (-80%)
-------------------------------------------------+-------------+ Total | 3.88 us (*) | 698 ns (-82%) ------------------------------------------------------------+-------------+--------------- fmt = bytearray(b"hello %s"); arg = b"x" * 10; fmt % arg | 661 ns (*) | 93 ns (-86%)
fmt = bytearray(b"hello %s"); arg = b"x" * 100; fmt % arg | 667 ns (*) | 93 ns (-86%)
fmt = bytearray(b"hello %s"); arg = b"x" * 10**3; fmt % arg | 982 ns (*) | 186 ns (-81%)
fmt = bytearray(b"hello %s"); arg = b"x" * 10**5; fmt % arg | 10.2 us (*) | 4.42 us (-57%)
------------------------------------------------------------+-------------+ Total | 12.5 us (*) | 4.8 us (-62%) --------------------------------------------------+-------------+--------------- fmt = bytearray(b"x" * 10 + b"%s"); fmt % b"y" | 653 ns (*) | 88 ns (-86%)
fmt = bytearray(b"x" * 100 + b"%s"); fmt % b"y" | 674 ns (*) | 94 ns (-86%)
fmt = bytearray(b"x" * 10**3 + b"%s"); fmt % b"y" | 1.09 us (*) | 213 ns (-80%)
fmt = bytearray(b"x" * 10**5 + b"%s"); fmt % b"y" | 21.4 us (*) | 8.47 us (-60%)
--------------------------------------------------+-------------+ Total | 23.8 us (*) | 8.87 us (-63%) ---------------------------------------------------------------------+-------------+-------- n = 200; fmt = bytearray(b"%f" * n); arg = tuple([1.2]*n); fmt % arg | 32.2 us (*) | 32.3 us
---------------------------------------------------------------------+-------------+ -----------------------------------------------------------------------+-------------+--------------- n = 1; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 678 ns (*) | 105 ns (-85%)
n = 5; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 884 ns (*) | 296 ns (-66%)
n = 10; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 1.13 us (*) | 531 ns (-53%)
n = 25; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 1.85 us (*) | 1.24 us (-33%)
n = 100; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 5.62 us (*) | 4.8 us (-15%)
n = 200; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 10.6 us (*) | 10.8 us
n = 500; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 25.1 us (*) | 26.8 us (+7%)
-----------------------------------------------------------------------+-------------+ Total | 45.9 us (*) | 44.6 us --------------------------------------------------------------------------+-------------+--------------- n = 1; fmt = bytearray(b"x=%d " * n); arg = tuple([12345]*n); fmt % arg | 699 ns (*) | 123 ns (-82%)
n = 5; fmt = bytearray(b"x=%d " * n); arg = tuple([12345]*n); fmt % arg | 943 ns (*) | 364 ns (-61%)
n = 10; fmt = bytearray(b"x=%d " * n); arg = tuple([12345]*n); fmt % arg | 1.22 us (*) | 655 ns (-47%)
n = 25; fmt = bytearray(b"x=%d " * n); arg = tuple([12345]*n); fmt % arg | 2.08 us (*) | 1.52 us (-27%)
n = 100; fmt = bytearray(b"x=%d " * n); arg = tuple([12345]*n); fmt % arg | 6.86 us (*) | 6.79 us
n = 200; fmt = bytearray(b"x=%d " * n); arg = tuple([12345]*n); fmt % arg | 12.6 us (*) | 13.3 us (+6%)
n = 500; fmt = bytearray(b"x=%d " * n); arg = tuple([12345]*n); fmt % arg | 29.7 us (*) | 32.4 us (+9%)
--------------------------------------------------------------------------+-------------+ Total | 54.1 us (*) | 55.2 us -----------------------------------------------------------------------+-------------+--------------- n = 1; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 677 ns (*) | 105 ns (-85%)
n = 5; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 886 ns (*) | 297 ns (-67%)
n = 10; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 1.13 us (*) | 530 ns (-53%)
n = 25; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 1.85 us (*) | 1.24 us (-33%)
n = 100; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 5.64 us (*) | 4.82 us (-15%)
n = 200; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 10.7 us (*) | 10.8 us
n = 500; fmt = bytearray(b"%d" * n); arg = tuple([12345]*n); fmt % arg | 25.2 us (*) | 26.8 us (+7%)
-----------------------------------------------------------------------+-------------+ Total | 46.1 us (*) | 44.6 us -----------------------------------------------------------------------------+-------------+--------------- n = 1; fmt = bytearray(b"x=%x " * n); arg = tuple([0xabcdef]*n); fmt % arg | 685 ns (*) | 120 ns (-82%)
n = 5; fmt = bytearray(b"x=%x " * n); arg = tuple([0xabcdef]*n); fmt % arg | 916 ns (*) | 342 ns (-63%)
n = 10; fmt = bytearray(b"x=%x " * n); arg = tuple([0xabcdef]*n); fmt % arg | 1.19 us (*) | 609 ns (-49%)
n = 25; fmt = bytearray(b"x=%x " * n); arg = tuple([0xabcdef]*n); fmt % arg | 1.99 us (*) | 1.41 us (-29%)
n = 100; fmt = bytearray(b"x=%x " * n); arg = tuple([0xabcdef]*n); fmt % arg | 6.64 us (*) | 6.43 us
n = 200; fmt = bytearray(b"x=%x " * n); arg = tuple([0xabcdef]*n); fmt % arg | 11.9 us (*) | 12.7 us (+7%)
n = 500; fmt = bytearray(b"x=%x " * n); arg = tuple([0xabcdef]*n); fmt % arg | 28.2 us (*) | 30.5 us (+8%)
-----------------------------------------------------------------------------+-------------+ Total | 51.5 us (*) | 52.1 us -------------------------------------------------------+-------------+--------------- fmt = bytearray(b"%i"); arg = 10 ** 0 - 1; fmt % arg | 651 ns (*) | 75 ns (-89%)
fmt = bytearray(b"%i"); arg = 10 ** 50 - 1; fmt % arg | 810 ns (*) | 245 ns (-70%)
fmt = bytearray(b"%i"); arg = 10 ** 100 - 1; fmt % arg | 1.06 us (*) | 496 ns (-53%)
fmt = bytearray(b"%i"); arg = 10 ** 150 - 1; fmt % arg | 1.38 us (*) | 819 ns (-41%)
fmt = bytearray(b"%i"); arg = 10 ** 200 - 1; fmt % arg | 1.87 us (*) | 1.28 us (-32%)
-------------------------------------------------------+-------------+ Total | 5.78 us (*) | 2.91 us (-50%) ---------------------------------------------------------+-------------+--------------- fmt = bytearray(b"x=%i"); arg = 10 ** 0 - 1; fmt % arg | 674 ns (*) | 103 ns (-85%)
fmt = bytearray(b"x=%i"); arg = 10 ** 50 - 1; fmt % arg | 820 ns (*) | 254 ns (-69%)
fmt = bytearray(b"x=%i"); arg = 10 ** 100 - 1; fmt % arg | 1.07 us (*) | 503 ns (-53%)
fmt = bytearray(b"x=%i"); arg = 10 ** 150 - 1; fmt % arg | 1.4 us (*) | 824 ns (-41%)
---------------------------------------------------------+-------------+ Total | 3.96 us (*) | 1.68 us (-58%) -------------------------+-------------+--------------- |
New changeset 03646293f1b3 by Victor Stinner in branch 'default': New changeset 6fe0050a2f52 by Victor Stinner in branch 'default': New changeset f369b79c0153 by Victor Stinner in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: