File: string_replace.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (47 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Copyright (c) 2021, Manfred Moitzi
# License: MIT License
import time
from ezdxf.tools.text_layout import lorem_ipsum
import re

REPLACE = {"Lorem": "", "ipsum": "", "dolor": "", "sit": ""}


def re_replace(rep, text):
    rep = dict((re.escape(k), v) for k, v in rep.items())
    pattern = re.compile("|".join(rep.keys()))
    pattern.sub(lambda m: rep[re.escape(m.group(0))], text)


def str_replace(rep, text):
    for old, new in rep.items():
        text = text.replace(old, new)


def find_substr(rep, text):
    for old, new in rep.items():
        text.find(old)


def contains_substr(rep, text):
    for old, new in rep.items():
        result = old in text


def profile(text, func, runs):
    content = " ".join(lorem_ipsum(1000))
    t0 = time.perf_counter()
    for _ in range(runs):
        func(REPLACE, content)
    t1 = time.perf_counter()
    t = t1 - t0
    print(f"{text} {t:.3f}s")


RUNS = 1_000

print(f"Profiling text replacement:")
profile(f"str_replace() {RUNS}:", str_replace, RUNS)
profile(f"re_replace() {RUNS}:", re_replace, RUNS)
profile(f"find_substr() {RUNS * 100}:", find_substr, RUNS * 100)
profile(f"contains_substr() {RUNS * 100}:", contains_substr, RUNS * 100)