Skip to content

Instantly share code, notes, and snippets.

@nascheme
Created May 29, 2025 19:31
Show Gist options
  • Select an option

  • Save nascheme/b5ef4a9c4fa4554ba8920d4710d4f007 to your computer and use it in GitHub Desktop.

Select an option

Save nascheme/b5ef4a9c4fa4554ba8920d4710d4f007 to your computer and use it in GitHub Desktop.
Benchmark for non-interned type lookup
import time
import threading
THREADS = 6
LOOPS = 200_000
class A:
x = 1
xx = 1
def run_lookup_interned():
a = A()
for i in range(LOOPS):
y = getattr(a, b'x'.decode('ascii'))
def run_lookup_non_interned():
a = A()
for i in range(LOOPS):
y = getattr(a, b'xx'.decode('ascii'))
def time_func(name, func):
t0 = time.perf_counter_ns()
threads = []
for i in range(THREADS):
t = threading.Thread(target=func)
threads.append(t)
t.start()
for t in threads:
t.join()
dt = (time.perf_counter_ns() - t0) / 1e6
print(name, f'{dt:.3f} ms')
def main():
time_func('interned', run_lookup_interned)
time_func('non-interned', run_lookup_non_interned)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment