Last active
February 7, 2021 07:33
-
-
Save mattip/aca31b74e5373fc6f7273950aa735949 to your computer and use it in GitHub Desktop.
benchmark for str.replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import pyperf | |
def create_data(fname='/tmp/lines.txt'): | |
printable = tuple(c for c in (chr(i) for i in range(32, 0x110000)) if c.isprintable() and not c.isspace()) | |
with open(fname, "w", encoding="utf8") as f: | |
for _ in range(10000): | |
f.write("".join(random.choices(printable, k=random.randint(1000, 10000)))) | |
f.write("\n") | |
def escape(s): | |
"""Replace the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in | |
the string with HTML-safe sequences. Use this if you need to display | |
text that might contain such characters in HTML. | |
If the object has an ``__html__`` method, it is called and the | |
return value is assumed to already be safe for HTML. | |
:param s: An object to be converted to a string and escaped. | |
:return: A :class:`Markup` string with the escaped text. | |
""" | |
if hasattr(s, "__html__"): | |
return str(s.__html__()) | |
return str( | |
str(s) | |
.replace("&", "&") | |
.replace(">", ">") | |
.replace("<", "<") | |
.replace("'", "'") | |
.replace('"', """) | |
) | |
def timeit(fname='/tmp/lines.txt'): | |
with open(fname, encoding="utf8") as f: | |
lines = f.readlines() | |
runner = pyperf.Runner() | |
runner.timeit( | |
name="native", | |
globals={"escape": escape, "lines": lines}, | |
stmt="for line in lines: escape(line)", | |
) | |
if __name__ == '__main__': | |
import os | |
fname = '/tmp/lines.txt' | |
if not os.path.exists(fname): | |
create_data(fname) | |
timeit(fname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment