STINNER Victor added the comment: bench_bytes_format.py: micro-benchmark testing a few formats. Some tests are focused on the implementation of _PyBytesWriter to ensure that the optimization is efficient.
Except of a single test (which is not really revelant, it takes less than 500 nanoseconds), all tests are faster. The b"xxxxxx %s" % b"y" test confirms that the optimization disabling overallocation for the last write is effective. Results: Common platform: Timer info: namespace(adjustable=False, implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, resolution=1e-09) Python unicode implementation: PEP 393 CPU model: Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz Platform: Linux-4.1.6-200.fc22.x86_64-x86_64-with-fedora-22-Twenty_Two CFLAGS: -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes Timer: time.perf_counter Bits: int=32, long=64, long long=64, size_t=64, void*=64 Platform of campaign orig: SCM: hg revision=1aae9b6a6929 tag=tip branch=default date="2015-10-09 01:34 -0400" Timer precision: 64 ns Python version: 3.6.0a0 (default:1aae9b6a6929, Oct 9 2015, 11:33:56) [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] Date: 2015-10-09 11:34:11 Platform of campaign writer: SCM: hg revision=fc2c11a19ae1+ tag=tip branch=default date="2015-10-09 11:48 +0200" Timer precision: 61 ns Python version: 3.6.0a0 (default:fc2c11a19ae1+, Oct 9 2015, 12:16:16) [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] Date: 2015-10-09 12:16:31 ---------------------------+------------+-------------- use smaller buffer | orig | writer ---------------------------+------------+-------------- b"hello %s" % b"world" | 13 ns (*) | 12 ns (-5%) b"hello %-100s" % b"world" | 158 ns (*) | 98 ns (-38%) b"x=%d" % 123 | 13 ns (*) | 12 ns b"x=%f" % 1.2 | 13 ns (*) | 13 ns b"x=%100d" % 123 | 156 ns (*) | 166 ns (+7%) ---------------------------+------------+-------------- Total | 353 ns (*) | 301 ns (-15%) ---------------------------+------------+-------------- -------------------------------------------------+-------------+--------------- "hello %s" % long_string | orig | writer -------------------------------------------------+-------------+--------------- fmt = b"hello %s"; arg = b"x" * 10; fmt % arg | 98 ns (*) | 86 ns (-12%) fmt = b"hello %s"; arg = b"x" * 100; fmt % arg | 85 ns (*) | 87 ns fmt = b"hello %s"; arg = b"x" * 10**3; fmt % arg | 298 ns (*) | 208 ns (-30%) fmt = b"hello %s"; arg = b"x" * 10**5; fmt % arg | 4.8 us (*) | 4.39 us (-9%) -------------------------------------------------+-------------+--------------- Total | 5.28 us (*) | 4.77 us (-10%) -------------------------------------------------+-------------+--------------- ---------------------------------------+-------------+--------------- b"xxxxxx %s" % b"y" | orig | writer ---------------------------------------+-------------+--------------- fmt = b"x" * 10 + b"%s"; fmt % b"y" | 99 ns (*) | 81 ns (-18%) fmt = b"x" * 100 + b"%s"; fmt % b"y" | 189 ns (*) | 87 ns (-54%) fmt = b"x" * 10**3 + b"%s"; fmt % b"y" | 1.12 us (*) | 209 ns (-81%) fmt = b"x" * 10**5 + b"%s"; fmt % b"y" | 88.4 us (*) | 8.49 us (-90%) ---------------------------------------+-------------+--------------- Total | 89.8 us (*) | 8.87 us (-90%) ---------------------------------------+-------------+--------------- ----------------------------------------------------------+-------------+--------------- %f | orig | writer ----------------------------------------------------------+-------------+--------------- n = 200; fmt = b"%f" * n; arg = tuple([1.2]*n); fmt % arg | 37.2 us (*) | 29.6 us (-21%) ----------------------------------------------------------+-------------+--------------- ------------------------------------------------------------+-------------+--------------- %i | orig | writer ------------------------------------------------------------+-------------+--------------- n = 200; fmt = b"%f" * n; arg = tuple([12345]*n); fmt % arg | 49.4 us (*) | 42.8 us (-13%) ------------------------------------------------------------+-------------+--------------- -------------------------+-------------+--------------- Summary | orig | writer -------------------------+-------------+--------------- use smaller buffer | 353 ns (*) | 301 ns (-15%) "hello %s" % long_string | 5.28 us (*) | 4.77 us (-10%) b"xxxxxx %s" % b"y" | 89.8 us (*) | 8.87 us (-90%) %f | 37.2 us (*) | 29.6 us (-21%) %i | 49.4 us (*) | 42.8 us (-13%) -------------------------+-------------+--------------- Total | 182 us (*) | 86.3 us (-53%) -------------------------+-------------+--------------- ---------- Added file: http://bugs.python.org/file40726/bench_bytes_format.py _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25349> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com