Skip to content

Instantly share code, notes, and snippets.

@tgamblin
Last active November 5, 2023 19:00
Show Gist options
  • Save tgamblin/e34e5a1daf5b16820a97e08a7944cc2e to your computer and use it in GitHub Desktop.
Save tgamblin/e34e5a1daf5b16820a97e08a7944cc2e to your computer and use it in GitHub Desktop.
#!/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)
@tgamblin
Copy link
Author

tgamblin commented Nov 5, 2023

very hard to beat just 4 chained replace calls:

> ./time-translate.py
dict translate
total time: 1.95s, 1,026,757/s

bytes translate
total time: 0.94s, 2,134,411/s

arr translate
total time: 0.92s, 2,183,725/s

4 chained replace calls
total time: 0.47s, 4,223,516/s

4 replace calls with assignment
total time: 0.61s, 3,264,245/s

replace loop
total time: 1.18s, 1,700,889/s

replace zip loop
total time: 1.29s, 1,553,982/s

re.sub
total time: 2.56s, 780,669/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment