[Python-Dev] Python 3.6 dict becomes compact and gets a private version; and keywords become ordered

Victor Stinner victor.stinner at gmail.com
Thu Sep 15 03:20:17 EDT 2016


2016-09-15 8:31 GMT+02:00 Serhiy Storchaka <storchaka at gmail.com>:
> Note that this is made at the expense of the 20% slowing down an iteration.
>
> $ ./python -m timeit -s "d = dict.fromkeys(range(10**6))" -- "list(d)"
> Python 3.5: 66.1 msec per loop
> Python 3.6: 82.5 msec per loop
>
> Fortunately the cost of the lookup (the most critical operation for dicts)
> seems left the same.
>
> But this can be an argument against using this technique in sets.

My small benchmarks on dict memory usage and dict lookup:


http://bugs.python.org/issue27350#msg275581

It seems like the memory usage is between 20% and 25% smaller. Great job!

Memory usage, Python 3.5 => Python 3.6 on Linux x86_64:

./python -c 'import sys; print(sys.getsizeof({str(i):i for i in range(10)}))'

* 10 items: 480 B => 384 B (-20%)
* 100 items: 6240 B => 4720 B (-24%)
* 1000 items: 49248 B => 36984 B (-25%)

Note: the size is the the size of the container itself, not of keys nor values.



http://bugs.python.org/issue27350#msg275587

As I expected, a dictionary lookup is a _little bit_ slower (3%)
between Python 3.5 and Python 3.6:

$ ./python -m perf timeit -s 'd={str(i):i for i in range(100)}'
'd["10"]; d["20"]; d["30"]; d["40"]; d["50"]; d["10"]; d["20"];
d["30"]; d["40"]; d["50"]' --rigorous

Median +- std dev: [lookup35] 309 ns +- 10 ns -> [lookup36] 320 ns +-
8 ns: 1.03x slower


Victor


More information about the Python-Dev mailing list