From ec21c6e84a89c6efeb76cd522ef2de7b21e0bf0d Mon Sep 17 00:00:00 2001 From: speedrun-program <71526906+speedrun-program@users.noreply.github.com> Date: Tue, 14 Sep 2021 15:49:00 -0700 Subject: [PATCH 1/2] use map function instead of genexpr in capwords In string.py, the capwords function passes str.join a generator expression, but the map function could be used instead. This is how capwords is currently written: -------------------- def capwords(s, sep=None): """ docstring text """ return (sep or ' ').join(x.capitalize() for x in s.split(sep)) -------------------- This is how capwords could be written: -------------------- def capwords(s, sep=None): """ docstring text """ return (sep or ' ').join(map(str.capitalize, s.split(sep))) -------------------- These are the benefits: 1. Faster performance which increases with the number of times the str is split. 2. Very slightly smaller .py and .pyc file sizes. 3. Source code is slightly more concise. This is the performance test code: -------------------- from timeit import timeit setup = """ def capwords_current(s, sep=None): return (sep or ' ').join(x.capitalize() for x in s.split(sep)) def capwords_new(s, sep=None): return (sep or ' ').join(map(str.capitalize, s.split(sep))) tests = ["a " * 10**n for n in range(9)] tests.append("a " * (10**9 // 2)) # I only have 16GB of RAM """ print("empty str without map:", timeit(setup=setup, stmt="x = capwords_current('')", number=1)) print("empty str with map :", timeit(setup=setup, stmt="x = capwords_new('')", number=1)) for n in range(9): print("- " * 20) print(f"10**{n} without map:", timeit(setup=setup, stmt=f"x = capwords_current(tests[{n}])", number=1)) print(f"10**{n} with map :", timeit(setup=setup, stmt=f"x = capwords_new(tests[{n}])", number=1)) print("- " * 20) print("10**9 // 2 without map:", timeit(setup=setup, stmt="x = capwords_current(tests[9])", number=1)) print("10**9 // 2 with map :", timeit(setup=setup, stmt="x = capwords_new(tests[9])", number=1)) print("done") -------------------- These are the results of a performance test: -------------------- empty str without map: 2.0000000000020002e-05 empty str with map : 1.8100000000020877e-05 - - - - - - - - - - - - - - - - - - - - 10**0 without map: 1.6600000000033255e-05 10**0 with map : 1.650000000008589e-05 - - - - - - - - - - - - - - - - - - - - 10**1 without map: 2.0399999999920482e-05 10**1 with map : 1.889999999993286e-05 - - - - - - - - - - - - - - - - - - - - 10**2 without map: 5.489999999985784e-05 10**2 with map : 4.6400000000001995e-05 - - - - - - - - - - - - - - - - - - - - 10**3 without map: 0.00026530000000013487 10**3 with map : 0.0001765000000002459 - - - - - - - - - - - - - - - - - - - - 10**4 without map: 0.0026298000000002375 10**4 with map : 0.0014880999999999922 - - - - - - - - - - - - - - - - - - - - 10**5 without map: 0.023361799999999988 10**5 with map : 0.016615499999999894 - - - - - - - - - - - - - - - - - - - - 10**6 without map: 0.24672029999999978 10**6 with map : 0.1923338999999995 - - - - - - - - - - - - - - - - - - - - 10**7 without map: 2.562209 10**7 with map : 1.8905919000000004 - - - - - - - - - - - - - - - - - - - - 10**8 without map: 26.3537843 10**8 with map : 18.781561099999998 - - - - - - - - - - - - - - - - - - - - 10**9 // 2 without map: 349.0668948 10**9 // 2 with map : 312.15139230000005 done -------------------- --- Lib/string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/string.py b/Lib/string.py index 489777b10c25df..261789cc10a44c 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -45,7 +45,7 @@ def capwords(s, sep=None): sep is used to split and join the words. """ - return (sep or ' ').join(x.capitalize() for x in s.split(sep)) + return (sep or ' ').join(map(str.capitalize, s.split(sep))) #################################################################### From 6669cf31d78fc3ac1bc0e73eb7265a49fb4bb5e9 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 16 Sep 2021 19:02:15 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-09-16-19-02-14.bpo-45225.xmKV4i.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-09-16-19-02-14.bpo-45225.xmKV4i.rst diff --git a/Misc/NEWS.d/next/Library/2021-09-16-19-02-14.bpo-45225.xmKV4i.rst b/Misc/NEWS.d/next/Library/2021-09-16-19-02-14.bpo-45225.xmKV4i.rst new file mode 100644 index 00000000000000..734fdd9b007d6b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-09-16-19-02-14.bpo-45225.xmKV4i.rst @@ -0,0 +1 @@ +use map function instead of genexpr in capwords. \ No newline at end of file