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`` a bit more. · Issue #107424 · python/cpython

Optimize textwrap.indent a bit more. #107424

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
Optimize textwrap.indent a bit more.#107424
Copy link
Labels
3.13only security fixesonly 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

@picnixz
picnixz
opened on Jul 29, 2023
Issue body actions

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

  • gh-107424: avoid using lambda functions in textwrap.indent() #107426

Activity

  1. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jul 29, 2023
  2. hugovk commented on Jul 29, 2023

    @hugovk
    hugovk
    on Jul 29, 2023
    Member
    More actions

    cc @methane

  3. AlexWaygood
    added
    performancePerformance or resource usagePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.13only security fixesonly security fixes
    on Jul 29, 2023
  4. picnixz
    added a commit that references this issue on Jul 29, 2023

    optimize textwrap.indent() (python#107424)

    3439066
  5. methane
    closed this as not plannedon Jul 30, 2023
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

    3.13only security fixesonly 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

    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.