Skip to content

Instantly share code, notes, and snippets.

@CGamesPlay
Created July 1, 2024 02:17
Show Gist options
  • Save CGamesPlay/c3bb3a2da8411f1cc50f788116091084 to your computer and use it in GitHub Desktop.
Save CGamesPlay/c3bb3a2da8411f1cc50f788116091084 to your computer and use it in GitHub Desktop.
MappingProxyType performance
import timeit
import types
def create_benchmark(test_dict):
def benchmark():
for key in range(100):
if key in test_dict:
value = test_dict[key]
return benchmark
# Benchmark normal dict
normal_dict = {key: str(key) for key in range(100)}
normal_benchmark = create_benchmark(normal_dict)
time_normal_dict = timeit.timeit(normal_benchmark, number=100000)
print(f"{time_normal_dict:.6e} s\tdict")
# Benchmark dict wrapped in MappingProxyType
proxy_dict = types.MappingProxyType({key: str(key) for key in range(100)})
proxy_benchmark = create_benchmark(proxy_dict)
time_proxy_dict = timeit.timeit(proxy_benchmark, number=100000)
print(f"{time_proxy_dict:.6e} s\tMappingProxyType")
# 2.607128e-01 s dict
# 2.819931e-01 s MappingProxyType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment