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 — Speed up regular expression substitution · Issue #91524 · python/cpython

Speed up regular expression substitution #91524

New issue
Copy link
New issue
Copy link
Closed
#91525
Closed
Speed up regular expression substitution#91524
#91525
Copy link
Labels
3.12only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-regextype-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Apr 14, 2022
Issue body actions

re.sub() is relatively slow, because for every match it calls a Python code.

Implementing it in C allows to speed up re.sub() to 2-3 times.

$ ./python -m timeit -s 'import re; s = "a"' 're.sub("(a)", r"\1", s)'
100000 loops, best of 5: 2.45 usec per loop
500000 loops, best of 5: 860 nsec per loop
$ ./python -m timeit -s 'import re; s = "a"; p = re.compile("(a)")' 'p.sub(r"\1", s)'
200000 loops, best of 5: 1.79 usec per loop
500000 loops, best of 5: 546 nsec per loop
$ ./python -m timeit -s 'import re; s = "a"*10**3' 're.sub("(a)", r"\1", s)'
500 loops, best of 5: 620 usec per loop
1000 loops, best of 5: 252 usec per loop
$ ./python -m timeit -s 'import re; s = "a"' 're.sub("(a)", r"b", s)'
500000 loops, best of 5: 711 nsec per loop
500000 loops, best of 5: 663 nsec per loop
$ ./python -m timeit -s 'import re; s = "a"' 're.sub("(a)", r"\n", s)'
200000 loops, best of 5: 1.7 usec per loop
500000 loops, best of 5: 864 nsec per loop

Initially I also implemented a public API for explicit compilation of the replacement string, but then left it to a separate issue.

Reacted by Shantanu, Erlend E. Aasland, Maciej Olko and Lee Alessandrini

Activity

  1. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-regex
    3.11only security fixesonly security fixes
    on Apr 14, 2022
  2. serhiy-storchaka
    added a commit that references this issue on Apr 14, 2022

    pythongh-91524: Speed up the regular expression substitution

    c213b37
  3. AlexWaygood
    added
    performancePerformance or resource usagePerformance or resource usage
    on Apr 14, 2022
  4. iritkatriel
    added
    3.12only security fixesonly security fixes
    and removed
    3.11only security fixesonly security fixes
    on Sep 7, 2022
  5. gpshead
    closed this as completedin #91525on Oct 23, 2022
  6. gpshead
    added a commit that references this issue on Oct 23, 2022

    gh-91524: Speed up the regular expression substitution (#91525)

    75a6fad
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.12only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-regextype-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.