GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Optimize textwrap.indent() · Issue #107369 · python/cpython

Optimize textwrap.indent() #107369

New issue
Copy link
New issue
Copy link
Closed
Closed
Optimize textwrap.indent()#107369
Copy link
Labels
performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory

Description

@methane
methane
opened on Jul 28, 2023
Issue body actions

Current code:

def indent(text, prefix, predicate=None):
    """Adds 'prefix' to the beginning of selected lines in 'text'.

    If 'predicate' is provided, 'prefix' will only be added to the lines
    where 'predicate(line)' is True. If 'predicate' is not provided,
    it will default to adding 'prefix' to all non-empty lines that do not
    consist solely of whitespace characters.
    """
    if predicate is None:
        def predicate(line):
            return line.strip()

    def prefixed_lines():
        for line in text.splitlines(True):
            yield (prefix + line if predicate(line) else line)
    return ''.join(prefixed_lines())
  • predicate = str.strip is faster than def predicate(line)
  • ''.join(x) converts input iterable to sequence. Using generator just makes overhead.
  • creating temporary prefix + line is avoidable.

Linked PRs

  • gh-107369: optimize textwrap.indent() #107374
  • gh-107369: Optimise textwrap.indent() #131923
Reacted by Goto Hayato

Activity

  1. methane
    added
    type-featureA feature request or enhancementA feature request or enhancement
    performancePerformance or resource usagePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    and removed
    type-featureA feature request or enhancementA feature request or enhancement
    on Jul 28, 2023
  2. methane commented on Jul 28, 2023

    @methane
    methane
    on Jul 28, 2023
    MemberAuthor
    More actions
    import timeit
    import textwrap
    
    with open("Objects/unicodeobject.c") as f:
        text = f.read()
    
    it = timeit.Timer(lambda: textwrap.indent(text, " "*4))
    result = it.repeat(number=1000)
    result.sort()
    print(f"indent {len(text.splitlines())} lines.")
    print(f"{result[0]:.4f}msec")

    before: 2.8msec
    after: 2.1msec (-25%)

  3. methane
    added a commit that references this issue on Jul 29, 2023

    gh-107369: optimize textwrap.indent() (#107374)

    37551c9
  4. sunmy2019
    closed this as completedon Jul 29, 2023
  5. AA-Turner
    added a commit that references this issue on Mar 31, 2025

    gh-107369: Optimise ``textwrap.indent()`` (#131923)

    51e0f2b
  6. hroncok commented on Apr 14, 2025

    @hroncok
    hroncok
    on Apr 14, 2025 · Hidden as off-topic
    show commentMore actions
  7. methane commented on Apr 15, 2025

    @methane
    methane
    on Apr 15, 2025
    MemberAuthor
    More actions

    @hroncok This issue is about indent, not dedent.

    See #131919 instead.

  8. hroncok commented on Apr 15, 2025

    @hroncok
    hroncok
    on Apr 15, 2025 · Hidden as resolved
    show commentMore actions
  9. hroncok commented on Apr 15, 2025

    @hroncok
    hroncok
    on Apr 15, 2025
    Contributor
    More actions

    Moved to #131919 (comment)

  10. methane commented on Apr 15, 2025

    @methane
    methane
    on Apr 15, 2025
    MemberAuthor
    More actions

    #131919 references this issue.

    It references #130167, not this issue.

    #131923 references this issue because it makes textwrap.indent faster again. It is unrelated to textwrap.dedent.

    Reacted by Miro Hrončok
  11. hroncok commented on Apr 15, 2025

    @hroncok
    hroncok
    on Apr 15, 2025
    Contributor
    More actions

    Sorry for my confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.