Skip to content

Instantly share code, notes, and snippets.

@mattip
Last active February 7, 2021 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattip/aca31b74e5373fc6f7273950aa735949 to your computer and use it in GitHub Desktop.
Save mattip/aca31b74e5373fc6f7273950aa735949 to your computer and use it in GitHub Desktop.
benchmark for str.replace
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("&", "&amp;")
.replace(">", "&gt;")
.replace("<", "&lt;")
.replace("'", "&#39;")
.replace('"', "&#34;")
)
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