Skip to content

Instantly share code, notes, and snippets.

@aeros
Last active November 14, 2019 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeros/ec2678304b45245a78310780fd47ec19 to your computer and use it in GitHub Desktop.
Save aeros/ec2678304b45245a78310780fd47ec19 to your computer and use it in GitHub Desktop.
Construction time benchmark for frozenmap (immutables.Map)
"""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')
@aeros
Copy link
Author

aeros commented Sep 20, 2019

Performance results:

OS: Arch Linux 5.3.0
CPU: Intel i5-4460
RAM: 16G

=============
  # of items: 1

  dict construction time: 0.00000321s
  ChainMap construction time: 0.00001516s
  frozenmap construction time: 0.00000386s
=============
  # of items: 5

  dict construction time: 0.00000279s
  ChainMap construction time: 0.00000553s
  frozenmap construction time: 0.00000340s
=============
  # of items: 10

  dict construction time: 0.00000360s
  ChainMap construction time: 0.00000689s
  frozenmap construction time: 0.00001401s
=============
  # of items: 100

  dict construction time: 0.00004714s
  ChainMap construction time: 0.00008749s
  frozenmap construction time: 0.00004118s
=============
  # of items: 1000

  dict construction time: 0.00031701s
  ChainMap construction time: 0.00051719s
  frozenmap construction time: 0.00054686s
=============
  # of items: 10000

  dict construction time: 0.00312743s
  ChainMap construction time: 0.00523761s
  frozenmap construction time: 0.00452464s
=============
  # of items: 100000

  dict construction time: 0.03632125s
  ChainMap construction time: 0.06007221s
  frozenmap construction time: 0.06947755s
=============
  # of items: 1000000

  dict construction time: 0.39704744s
  ChainMap construction time: 0.61989591s
  frozenmap construction time: 2.51226446s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment