Last active
November 14, 2019 08:36
-
-
Save aeros/ec2678304b45245a78310780fd47ec19 to your computer and use it in GitHub Desktop.
Construction time benchmark for frozenmap (immutables.Map)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Created for the purpose of comparing the construction time | |
between ``dict``, ``ChainMap``, and the proposed ``frozenmap`` in PEP-603. | |
""" | |
import sys | |
import time | |
import collections | |
import immutables | |
# Could improve accuracy by running multiple iterations per test | |
# and getting the average rather than a single run for each N | |
for N in [1, 5, 10, 100, 1_000, 10_000, 100_000, 1_000_000]: | |
print('=============') | |
print(f' # of items: {N}') | |
print() | |
start = time.monotonic() | |
d = dict() | |
for i in range(N): | |
d[chr(i)] = i | |
total = time.monotonic() - start | |
assert(len(d) == N) | |
print(f' dict construction time: {total:.8f}s') | |
start = time.monotonic() | |
c = collections.ChainMap() | |
for i in range(N): | |
c[chr(i)] = i | |
total = time.monotonic() - start | |
assert(len(c) == N) | |
print(f' ChainMap construction time: {total:.8f}s') | |
start = time.monotonic() | |
h = immutables.Map() | |
hm = h.mutate() | |
for i in range(N): | |
hm[chr(i)] = i | |
h = hm.finish() | |
total = time.monotonic() - start | |
assert(len(h) == N) | |
print(f' frozenmap construction time: {total:.8f}s') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Performance results:
OS: Arch Linux 5.3.0
CPU: Intel i5-4460
RAM: 16G