Skip to content

Instantly share code, notes, and snippets.

@1st1
Last active April 3, 2018 17:19
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 1st1/57d979e1e58948a7e523754a3d02a57d to your computer and use it in GitHub Desktop.
Save 1st1/57d979e1e58948a7e523754a3d02a57d to your computer and use it in GitHub Desktop.
  1. $ git clone https://github.com/MagicStack/immutables.git
  2. cd immutables
  3. wget https://gist.githubusercontent.com/1st1/57d979e1e58948a7e523754a3d02a57d/raw/8857aca8e0f13c804218540820993b6481cd6619/bench.py
  4. pypy3 bench.py -- observe the timings for "hamt"
  5. run pypy3 bench.py -- observe how "hamt" timings are way worse now
  6. find . -name '*.pyc' | xargs rm
  7. run pypy3 bench.py -- observe how "hamt" timings are low again
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()
for i in range(N):
h = h.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")

first run

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

  dict copy:			0.1487s
  hamt:				0.6501s
=============
  # of items: 10; iterations: 1000000

  dict copy:			0.3177s
  hamt:				0.6005s
=============
  # of items: 20; iterations: 1000000

  dict copy:			0.4995s
  hamt:				0.7768s
=============
  # of items: 30; iterations: 1000000

  dict copy:			0.6714s
  hamt:				0.9609s
=============
  # of items: 100; iterations: 1000000

  dict copy:			2.2390s
  hamt:				0.9238s
=============

[etc]

second run

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

  dict copy:			0.1543s
  hamt:				3.9164s
=============
  # of items: 10; iterations: 1000000

  dict copy:			0.3109s
  hamt:				6.4567s
=============
  # of items: 20; iterations: 1000000

  dict copy:			0.4718s
  hamt:				6.6128s
=============
[etc]

pypy3 --version

Python 3.5.3 (3f6eaa010fce, Jan 11 2018, 05:27:47)
[PyPy 5.10.1 with GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]

More details:

I'm running this all under pypy3 venv created with pypy3 -m venv test.

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