Skip to content

Instantly share code, notes, and snippets.

@cfbolz
Created May 8, 2024 17:11
Show Gist options
  • Save cfbolz/626555a7d53ed7fbfa39e4849b1fbee3 to your computer and use it in GitHub Desktop.
Save cfbolz/626555a7d53ed7fbfa39e4849b1fbee3 to your computer and use it in GitHub Desktop.
Demonstrate the problem that using blackhole is quite tricky
try:
import pypyjit
except ImportError:
def blackbox(x):
return x
else:
def identity(x):
return x
def blackbox(x):
return pypyjit.residual_call(identity, x)
def main():
import time
t1 = time.perf_counter()
for i in range(10_000_000):
pass
t2 = time.perf_counter()
print("empty loop", t2 - t1)
t1 = time.perf_counter()
for i in range(10_000_000):
blackbox(1)
t2 = time.perf_counter()
print("empty loop with blackbox call", t2 - t1)
t1 = time.perf_counter()
for i in range(10_000_000):
l = "abcdefghi"
l.index("ghi") # constant-folded
t2 = time.perf_counter()
print("constant-folded", t2 - t1)
t1 = time.perf_counter()
substring = blackbox("ghi")
for i in range(10_000_000):
l = "abcdefghi"
l.index(substring) # can't constant-fold, but it's loop invariant
t2 = time.perf_counter()
print("loop-invariant, despite blackbox", t2 - t1)
t1 = time.perf_counter()
substring = "ghi"
for i in range(10_000_000):
l = "abcdefghi"
l.index(blackbox(substring)) # completely unrealistic overhead due to calling blackbox
t2 = time.perf_counter()
print("no cheating, but distorted", t2 - t1)
main()
# my laptop, pypy3.10:
# empty loop 0.005483658984303474
# empty loop with blackbox call 0.7468732330016792
# constant-folded 0.004697702941484749
# loop-invariant, despite blackbox 0.09397784899920225
# no cheating, but distorted 0.8346890190150589
# python3.11
# empty loop 0.07850259297993034
# empty loop with blackbox call 0.23288042598869652
# constant-folded 0.4955052570439875
# loop-invariant, despite blackbox 0.5159864641027525
# no cheating, but distorted 0.6361238369718194
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment