Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
Closed as not planned
Labels
3.13bugs and security fixesbugs and security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
The optimization introduced in #107374 can be improved as follows:
Current code
ifpredicateisNone: predicate=lambdas: nots.isspace() prefixed_lines= [] forlineintext.splitlines(True): ifpredicate(line): prefixed_lines.append(prefix) prefixed_lines.append(line)Improved code
prefixed_lines= [] ifpredicateisNone: forlineintext.splitlines(True): ifnotline.isspace(): prefixed_lines.append(prefix) prefixed_lines.append(line) else: forlineintext.splitlines(True): ifpredicate(line): prefixed_lines.append(prefix) prefixed_lines.append(line)Benchmarks
importtimeitimporttextwrapwithopen("Objects/unicodeobject.c") asf: text=f.read() print(f"indent {len(text.splitlines())} lines.") it=timeit.Timer(lambda: textwrap.indent(text, ' '*4)) result=it.repeat(number=1000) result.sort() print(f"{result[0]:.4f}msec")- Current: 7.9305msec
- After: 6.7143msec
However the results are very different between two runs (and my laptop may be dying) so I'd like some confirmation from others.
Linked PRs
Metadata
Metadata
Assignees
Labels
3.13bugs and security fixesbugs and security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement