Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use re instead of pyparsing for "cleaning color" #1411

Merged
merged 1 commit into from
Oct 16, 2024

Conversation

zrquan
Copy link
Contributor

@zrquan zrquan commented Oct 16, 2024

Description

The overhead of pyparsing is quite high; we can use re to implement the clean_color function.

I ran a simple benchmark using hyperfine.

Benchmark 1: python use_re.py
  Time (mean ± σ):      48.0 ms ±   3.2 ms    [User: 39.0 ms, System: 8.8 ms]
  Range (min … max):    40.7 ms …  56.8 ms    59 runs

Benchmark 2: python use_pyparsing.py
  Time (mean ± σ):      3.542 s ±  0.088 s    [User: 3.521 s, System: 0.014 s]
  Range (min … max):    3.471 s …  3.770 s    10 runs

Summary
  python use_re.py ran
   73.75 ± 5.27 times faster than python use_pyparsing.py

use_re.py

import re

_ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")

def clean_color(msg):
    return _ansi_escape.sub("", msg)

for _ in range(10000):
    msg = "\x1b[1;31m\x1b[40mthis is a message for testing"
    clean_color(msg)

use_pyparsing.py

import string
from pyparsing import Combine, Literal, Optional, Suppress, Word, delimitedList, oneOf

_escape_seq = Combine(
    Literal("\x1b")
    + "["
    + Optional(delimitedList(Word(string.digits), ";"))
    + oneOf(list(string.ascii_letters))
)

def clean_color(msg):
    return Suppress(_escape_seq).transformString(msg)

for _ in range(10000):
    msg = "\x1b[1;31m\x1b[40mthis is a message for testing"
    clean_color(msg)

Requirements

  • Add your name to CONTRIBUTORS.md
  • If this is a new feature, then please add some additional information about it to CHANGELOG.md

@shelld3v shelld3v merged commit 6c7e726 into maurosoria:master Oct 16, 2024
9 checks passed
@zrquan zrquan deleted the patch-perf branch October 17, 2024 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants