-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
use map function instead of genexpr in capwords #89388
Comments
In string.py, the capwords function passes str.join a generator expression, but the map function -------------------- def capwords(s, sep=None):
"""capwords(s [,sep]) -> string
Split the argument into words using split, capitalize each
word using capitalize, and join the capitalized words using
join. If the optional second argument sep is absent or None,
runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise
sep is used to split and join the words.
"""
return (sep or ' ').join(x.capitalize() for x in s.split(sep)) -------------------- This is how capwords could be written: -------------------- def capwords(s, sep=None):
"""capwords(s [,sep]) -> string
Split the argument into words using split, capitalize each
word using capitalize, and join the capitalized words using
join. If the optional second argument sep is absent or None,
runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise
sep is used to split and join the words.
"""
return (sep or ' ').join(map(str.capitalize, s.split(sep))) -------------------- These are the benefits:
This is the performance test code in ipython: -------------------- 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 -------------------- These are the results of a performance test using %timeit in ipython: -------------------- %timeit x = capwords_current("") %timeit x = capwords_new("") %timeit x = capwords_current(tests[0]) %timeit x = capwords_new(tests[0]) %timeit x = capwords_current(tests[1]) %timeit x = capwords_new(tests[1]) %timeit x = capwords_current(tests[2]) %timeit x = capwords_new(tests[2]) %timeit x = capwords_current(tests[3]) %timeit x = capwords_new(tests[3]) %timeit x = capwords_current(tests[4]) %timeit x = capwords_new(tests[4]) %timeit x = capwords_current(tests[5]) %timeit x = capwords_new(tests[5]) %timeit x = capwords_current(tests[6]) %timeit x = capwords_new(tests[6]) %timeit x = capwords_current(tests[7]) %timeit x = capwords_new(tests[7]) %timeit x = capwords_current(tests[8]) %timeit x = capwords_new(tests[8]) %timeit x = capwords_current(tests[9]) %timeit x = capwords_new(tests[9]) -------------------- |
Thanks for the PR. |
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: