Skip to content

Optimize textwrap.indent a bit more. #107424

@picnixz

Description

@picnixz

The optimization introduced in #107374 can be improved as follows:

Current code

if predicate is None:
    predicate = lambda s: not s.isspace()

    prefixed_lines = []
    for line in text.splitlines(True):
        if predicate(line):
            prefixed_lines.append(prefix)
        prefixed_lines.append(line)

Improved code

prefixed_lines = []

if predicate is None:
    for line in text.splitlines(True):
        if not line.isspace():
            prefixed_lines.append(prefix)
        prefixed_lines.append(line)
else:
    for line in text.splitlines(True):
        if predicate(line):
            prefixed_lines.append(prefix)
        prefixed_lines.append(line)

Benchmarks

import timeit
import textwrap

with open("Objects/unicodeobject.c") as f:
    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

No one assigned

    Labels

    3.13bugs and security fixesperformancePerformance or resource usagestdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy