Last active
November 5, 2023 19:00
-
-
Save tgamblin/e34e5a1daf5b16820a97e08a7944cc2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import re | |
import time | |
import contextlib | |
@contextlib.contextmanager | |
def timeit(): | |
start = time.time() | |
try: | |
yield | |
finally: | |
end = time.time() | |
print(f"total time: {end - start:.2f}s") | |
with open("hashes") as f: | |
hashes = f.read().strip().split() | |
N = 10000 | |
translator = str.maketrans("oliu", "0189") | |
bytes_translator = bytes.maketrans(b"oliu", b"0189") | |
arr_translator = list(range(128)) | |
for k, v in zip("oliu", "0189"): | |
arr_translator[ord(k)] = ord(v) | |
print("dict translate") | |
with timeit(): | |
for i in range(N): | |
for h in hashes: | |
h.translate(translator) | |
print() | |
print("bytes translate") | |
with timeit(): | |
for i in range(N): | |
for h in hashes: | |
h.translate(bytes_translator) | |
print() | |
print("arr translate") | |
with timeit(): | |
for i in range(N): | |
for h in hashes: | |
h.translate(arr_translator) | |
print() | |
print("4 replace calls") | |
with timeit(): | |
for i in range(N): | |
for h in hashes: | |
h = h.replace("o", "0").replace("l", "1").replace("i", "8").replace("u", "9") | |
print() | |
print("replace loop") | |
with timeit(): | |
for i in range(N): | |
for h in hashes: | |
for k, v in ("o0", "l1", "i8", "u9"): | |
h = h.replace(k, v) | |
print() | |
print("replace zip loop") | |
with timeit(): | |
for i in range(N): | |
for h in hashes: | |
for k, v in zip("oliu", "0189"): | |
h = h.replace(k, v) | |
t = {"o": "0", "l": "1", "i": "8", "u": "9"} | |
def replacer(m): | |
g = m.group(0) | |
return t.get(g, g) | |
print() | |
print("re.sub") | |
with timeit(): | |
for i in range(N): | |
for h in hashes: | |
re.sub(r"[oliu]", replacer, h) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very hard to beat just 4 chained replace calls: