C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Version 1: In this loop we do a get() for the first key. The get method internally computes a hash, but only needs to scan a short string.
Version 2: Here we call get() on the longer string. In each iteration, a hash code must be computed for the entire string.
Python program that times short, long string key lookups
import time
lookup = {"cat": 1, "anextremelylongstringkey": 2}
print(time.time())
# Version 1: short string key.
for i in range(0, 100000000):
v = lookup.get("cat")
print(time.time())
# Version 2: long string key.
for i in range(0, 100000000):
v = lookup.get("anextremelylongstringkey")
print(time.time())
Output: PyPy 100 million iterations
1412466713.551
1412466715.161 Get short key = 1.61 s
1412466716.911 Get long key = 1.75 s
Output: Python3 10 million iterations
1412467011.994969
1412467014.838859 Short = 2.84 s
1412467017.745185 Long = 2.91 s
Thus: Using the shortest unique keys possible in a dictionary will improve performance.
Analysis: This is not a primary consideration, but performance tips like this one can help us develop better programs over time.
Dictionary