Skip to content

Instantly share code, notes, and snippets.

@1st1
Last active November 1, 2022 11:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 1st1/292e3f0bbe43bd65ff3256f80aa2637d to your computer and use it in GitHub Desktop.
Save 1st1/292e3f0bbe43bd65ff3256f80aa2637d to your computer and use it in GitHub Desktop.
immutables benchmark
import pyrsistent
import immutables
import time
I = 1_000_000
KEY = '5'
for N in [5, 10, 20, 30, 100, 200, 300, 400, 500, 1000]:
print('=============')
print(f' # of items: {N}; iterations: {I}')
print()
h = immutables.Map()
d = dict()
pm = pyrsistent.m()
for i in range(N):
h = h.set(str(i), i)
pm = pm.set(str(i), i)
d[str(i)] = i
assert len(h) == N
for i in range(N):
assert h.get(str(i), 'not found') == i
st = time.monotonic()
for _ in range(I):
d.get(KEY)
d.get(KEY)
d.get(KEY)
d.get(KEY)
d.get(KEY)
d.get(KEY)
d.get(KEY)
d.get(KEY)
d.get(KEY)
d.get(KEY)
d2 = d.copy()
d2['aaa'] = 'aaa'
end = time.monotonic() - st
print(f" dict copy:\t\t\t{end:.4f}s")
st = time.monotonic()
for _ in range(I):
h.get(KEY)
h.get(KEY)
h.get(KEY)
h.get(KEY)
h.get(KEY)
h.get(KEY)
h.get(KEY)
h.get(KEY)
h.get(KEY)
h.get(KEY)
h2 = h.set('aaa', 'aaa')
end = time.monotonic() - st
print(f" hamt:\t\t\t\t{end:.4f}s")
st = time.monotonic()
for _ in range(I):
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm.get(KEY)
pm2 = pm.set('aaa', 'aaa')
end = time.monotonic() - st
print(f" PMap:\t\t\t\t{end:.4f}s")
@1st1
Copy link
Author

1st1 commented Apr 2, 2018

Results

=============
  # of items: 5; iterations: 1000000

  dict copy:			1.4610s
  hamt:				1.6633s
  PMap:				17.0337s
=============
  # of items: 10; iterations: 1000000

  dict copy:			1.8299s
  hamt:				1.7645s
  PMap:				14.2364s
=============
  # of items: 20; iterations: 1000000

  dict copy:			2.0427s
  hamt:				1.7944s
  PMap:				14.3527s
=============
  # of items: 30; iterations: 1000000

  dict copy:			2.1659s
  hamt:				1.8046s
  PMap:				14.6958s
=============
  # of items: 100; iterations: 1000000

  dict copy:			3.5980s
  hamt:				1.8421s
  PMap:				15.2095s
=============
  # of items: 200; iterations: 1000000

  dict copy:			5.7485s
  hamt:				1.8876s
  PMap:				16.0858s
=============
  # of items: 300; iterations: 1000000

  dict copy:			8.2082s
  hamt:				1.8247s
  PMap:				16.9278s
=============
  # of items: 400; iterations: 1000000

  dict copy:			10.2575s
  hamt:				1.9317s
  PMap:				17.3764s
=============
  # of items: 500; iterations: 1000000

  dict copy:			12.6132s
  hamt:				2.1112s
  PMap:				17.1382s
=============
  # of items: 1000; iterations: 1000000

  dict copy:			24.0334s
  hamt:				2.2156s
  PMap:				15.9785s

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