Skip to content

Instantly share code, notes, and snippets.

@1st1
Created Sep 9, 2019
Embed
What would you like to do?
import pyrsistent
import immutables
import time
I = 1000000
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" immutables.Map:\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

This comment has been minimized.

Copy link
Owner Author

@1st1 1st1 commented Sep 9, 2019

Results on Macbook Pro 2017 / 2.9 GHz Intel Core i7 / 16Gb:

{immu37} ~/d/m/immutables (master %) » python bench.py
=============
  # of items: 5; iterations: 1000000

  dict copy:			1.1134s
  immutables.Map:		1.2374s
  PMap:				16.4141s
=============
  # of items: 10; iterations: 1000000

  dict copy:			1.2713s
  immutables.Map:		1.6194s
  PMap:				14.1385s
=============
  # of items: 20; iterations: 1000000

  dict copy:			1.2402s
  immutables.Map:		1.8200s
  PMap:				14.3209s
=============
  # of items: 30; iterations: 1000000

  dict copy:			1.2726s
  immutables.Map:		1.7884s
  PMap:				14.0487s
=============
  # of items: 100; iterations: 1000000

  dict copy:			1.4538s
  immutables.Map:		1.7611s
  PMap:				13.4741s
=============
  # of items: 200; iterations: 1000000

  dict copy:			1.9769s
  immutables.Map:		1.8063s
  PMap:				13.7524s
=============
  # of items: 300; iterations: 1000000

  dict copy:			2.5515s
  immutables.Map:		1.8675s
  PMap:				14.7535s
=============
  # of items: 400; iterations: 1000000

  dict copy:			3.1378s
  immutables.Map:		1.8667s
  PMap:				14.8536s
=============
  # of items: 500; iterations: 1000000

  dict copy:			3.4587s
  immutables.Map:		1.9014s
  PMap:				15.2210s
=============
  # of items: 1000; iterations: 1000000

  dict copy:			7.5077s
  immutables.Map:		1.7825s
  PMap:				14.7813s
@aeros

This comment has been minimized.

Copy link

@aeros aeros commented Sep 20, 2019

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

=============
  # of items: 5; iterations: 1000000
  dict copy:                    0.9841s
  immutables.Map:               1.2221s
  PMap:                         15.1024s
=============
  # of items: 10; iterations: 1000000
  dict copy:                    1.0641s
  immutables.Map:               1.4830s
  PMap:                         11.4983s
=============
  # of items: 20; iterations: 1000000
  dict copy:                    1.0052s
  immutables.Map:               1.4763s
  PMap:                         11.2620s
=============
  # of items: 30; iterations: 1000000
  dict copy:                    1.0856s
  immutables.Map:               1.5449s
  PMap:                         11.0159s
=============
  # of items: 100; iterations: 1000000
  dict copy:                    1.3583s
  immutables.Map:               1.5678s
  PMap:                         12.1514s
=============
  # of items: 200; iterations: 1000000
  dict copy:                    2.1822s
  immutables.Map:               1.6834s
  PMap:                         12.6607s
=============
  # of items: 300; iterations: 1000000
  dict copy:                    2.9123s
  immutables.Map:               1.7050s
  PMap:                         12.4604s
=============
  # of items: 400; iterations: 1000000
  dict copy:                    4.1913s
  immutables.Map:               1.8522s
  PMap:                         12.4791s
=============
  # of items: 500; iterations: 1000000
  dict copy:                    4.9216s
  immutables.Map:               1.8017s
  PMap:                         12.6718s
=============
  # of items: 1000; iterations: 1000000
  dict copy:                    9.3672s
  immutables.Map:               1.7347s
  PMap:                         12.9271s

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