perf: optimize lower with list comprehension - #15229
Closed
Miladkhoshdel wants to merge 2 commits into
Closed
Conversation
Miladkhoshdel
force-pushed
the
perf/optimize-lower-comprehension
branch
3 times, most recently
from
September 8, 2026 16:49
99d8001 to
7f410e9
Compare
Miladkhoshdel
force-pushed
the
perf/optimize-lower-comprehension
branch
2 times, most recently
from
September 8, 2026 16:57
776d31b to
3dba0bc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Optimize the existing ASCII lower() implementation by replacing the explicit append loop with a list comprehension.
The updated implementation preserves the existing behavior while improving runtime performance.
Benchmark results using timeit.repeat show a median improvement of roughly 9–13% across the tested input sizes.
Benchmark code
from statistics import median
from timeit import repeat
ASCII_UPPERCASE_START = ord("A")
ASCII_UPPERCASE_END = ord("Z")
ASCII_CASE_OFFSET = ord("a") - ord("A")
def new_lower(word: str) -> str:
"""
Convert ASCII uppercase letters in a string to lowercase.
def old_lower(word: str) -> str:
"""
Convert ASCII uppercase letters in a string to lowercase.
def benchmark(
text: str,
number: int = 1_000,
repeats: int = 10,
) -> None:
"""Benchmark the old and new implementations."""
assert old_lower(text) == new_lower(text)
if name == "main":
sample = "Hello WORLD 123! "
Benchmark results
python3 benchmark_lower.py
Input length: 170
Iterations: 1,000
Repeats: 10
Old: min=0.008750s, median=0.008759s, max=0.008812s
New: min=0.007604s, median=0.007921s, max=0.010934s
Median speedup: 1.11x
Median improvement: 9.57%
Input length: 1,700
Iterations: 1,000
Repeats: 10
Old: min=0.082310s, median=0.082881s, max=0.084548s
New: min=0.071824s, median=0.072757s, max=0.073110s
Median speedup: 1.14x
Median improvement: 12.22%
Input length: 17,000
Iterations: 1,000
Repeats: 10
Old: min=0.807068s, median=0.810171s, max=0.828573s
New: min=0.704969s, median=0.710480s, max=0.771348s
Median speedup: 1.14x
Median improvement: 12.30%
Input length: 170,000
Iterations: 1,000
Repeats: 10
Old: min=7.946771s, median=7.990341s, max=8.148172s
New: min=6.880901s, median=6.913833s, max=6.931525s
Median speedup: 1.16x
Median improvement: 13.47%
Checklist: