Debian Codegraph @

docs/performance.rst

raw
1 Performance Comparison
2 ======================
3
4 Measuring performance is a difficult task. All the benchmarks on this page are
5 synthetic in the sense that they test one function repeatedly. Measurements in
6 live systems are much harder to produce reliably. So take the following data
7 with a grain of salt. That being said, a stated feature of :doc:`Sorted
8 Containers<index>` is performance so we would be remiss not to produce this
9 page with comparisons.
10
11 The source for all benchmarks can be found under the "tests" directory in the
12 files prefixed "benchmark." Measurements are made from the min, max, and median
13 of 5 repetitions. In the graphs below, the line follows the median at each
14 point. Note that the axes are log-log so properly reading two different lines
15 would describe one metric as "X times" faster rather than "X seconds"
16 faster. In all graphs, lower is better. Measurements are made by powers of ten:
17 100 through 10,000,000.
18
19 Measurements up to ten billion elements have been successfully tested and
20 benchmarked. Read :doc:`performance-scale` for details. Only a couple
21 implementations (including :doc:`Sorted Containers<index>`) are capable of
22 handling so many elements. The major limiting factor at that size is
23 memory. Consider the simple case of storing CPython's integers in a
24 :doc:`sortedlist`. Each integer object requires ~24 bytes so one hundred
25 million elements will require about three gigabytes of memory. If the
26 implementation adds significant overhead then most systems will run out of
27 memory. For all datasets which may be kept in memory, :doc:`Sorted
28 Containers<index>` is an excellent choice.
29
30 A good effort has been made to find competing implementations. Seven in total
31 were found with various list, set, and dict implementations.
32
33 1. *blist* -- Provides list, dict, and set containers based on the blist
34 data-type. Uses a `B-Tree`_ data structure. Implemented in Python and C. BSD
35 License. Last updated March, 2014. `blist on PyPI`_
36
37 2. *bintrees* -- Provides several tree-based implementations for dict and set
38 containers. Fastest were AVL-Tree and Red-Black-Tree data
39 structures.. Extends the conventional API to provide set operations for the
40 dict type. Now deprecated in favor of :doc:`Sorted Containers<index>`
41 Implemented in C. MIT License. Last updated April, 2017. `bintrees on
42 PyPI`_
43
44 3. *sortedmap* -- Provides a fast, C++ implementation for dict data types. Uses
45 the C++ standard library `std::map` data structure which is usually a
46 red-black tree. Last updated February, 2016. `sortedmap on PyPI`_
47
48 4. *banyan* -- Provides a fast, C++ implementation for dict and set data
49 types. Offers some features also found in sortedcontainers like accessing
50 the n-th item in a set or dict. Uses sources from the `tree implementation`_
51 in GNU libstdc++. GPLv3 License. Last updated April, 2013. `banyan on PyPI`_
52
53 5. *treap* -- Uses Cython for improved performance and provides a dict
54 container. Apache V2 License. Last updated June, 2017. `treap on PyPI`_
55
56 6. *skiplistcollections* -- Pure-Python implementation based on skip-lists
57 providing a limited API for dict and set types. MIT License. Last updated
58 January, 2014. `skiplistcollections on PyPI`_
59
60 7. *sortedcollection* -- Pure-Python implementation of sorted list based solely
61 on a list. Feature-poor and inefficient for writes but included because it
62 is written by Raymond Hettinger and linked from the official Python
63 docs. MIT License. Last updated April, 2011. `sortedcollection recipe`_
64
65 Several alternative implementations were omitted for reasons documented below:
66
67 A. *rbtree* -- C-implementation that only supports Python 2. Provides a fast,
68 C-implementation for dict and set data types. GPLv3 License. Last updated
69 March, 2012. `rbtree on PyPI`_
70
71 B. *ruamel.ordereddict.sorteddict* -- C-implementation that only supports
72 Python 2. Performance was measured in correspondence with the module
73 author. Performance was generally very good except for ``__delitem__``. At
74 scale, deleting entries became exceedingly slow. MIT License. Last updated
75 July, 2017. `ruamel.ordereddict on PyPI`_
76
77 C. *pyskiplist* -- Pure-Python skip-list based implementation supporting a
78 sorted-list-like interface. Now deprecated in favor of :doc:`Sorted
79 Containers<index>`. MIT License. Last updated July, 2015. `pyskiplist on
80 PyPI`_
81
82 D. *sorteddict* -- Pure-Python lazily-computed sorted dict implementation. Now
83 deprecated in favor of :doc:`Sorted Containers<index>`. GPLv3 License. Last
84 updated September, 2007. `sorteddict on PyPI`_
85
86 E. *rbtree from NewCenturyComputers* -- Pure-Python tree-based
87 implementation. Not sure when this was last updated. Unlikely to be
88 fast. Unknown license. Unknown last update. `rbtree from
89 NewCenturyComputers`_
90
91 F. *python-avl-tree from GitHub user pgrafov* -- Pure-Python tree-based
92 implementation. Unlikely to be fast. MIT License. Last updated
93 October, 2010. `python-avl-tree from GitHub user pgrafov`_
94
95 G. *pyavl* -- C-implementation for AVL tree-based dict and set
96 containers. Claims to be fast. Lacking documentation and failed to
97 build. Public Domain License. Last updated December, 2008. `pyavl on PyPI`_
98
99 H. *skiplist* -- C-implementation of sorted list based on skip-list data
100 structure. Only supports Python 2. Zlib/libpng License. Last updated
101 Septemeber, 2013. `skiplist from Bitbucket user mojaves`_
102
103 The most similar module to :doc:`Sorted Containers<index>` is
104 skiplistcollections given that each is implemented in Python. But as is
105 displayed below, Sorted Containers is several times faster and provides a
106 richer API. Often the pure-Python implementation in Sorted Containers is faster
107 even than the C-implementation counterparts. Where it lacks, performance is
108 generally on the same magnitude.
109
110 Because :doc:`Sorted Containers<index>` is implemented in pure-Python, its
111 performance depends directly on the Python runtime. A :doc:`runtime performance
112 comparison<performance-runtime>` is also included with data from popular
113 runtimes.
114
115 :doc:`Sorted Containers<index>` uses a segmented-list data structure similar to
116 a B-tree limited to two levels of nodes. As part of the implementation, a load
117 factor is used to determine how many values should be stored in each node. This
118 can have a significant impact on performance and a :doc:`load factor
119 performance comparison<performance-load>` is also provided.
120
121 Though these benchmarks exercise only one API repeatedly, an effort has also
122 been made to simulate real-world workloads. The :doc:`simulated workload
123 performance comparison<performance-workload>` contains examples with
124 comparisons to other implementations, load factors, and runtimes.
125
126 Some final notes about the graphs below. Missing data indicates the benchmark
127 either took too long or failed. The set operations with tiny, small, medium,
128 and large variations indicate the size of the container involved in the
129 right-hand-side of the operation: tiny is exactly 10 elements; small is 10% of
130 the size of the left-hand-side; medium is 50%; and large is 100%. :doc:`Sorted
131 Containers<index>` uses a different algorithm based on the size of the
132 right-hand-side of the operation for a dramatic improvement in performance.
133
134 The legends of the graphs below correlate the underlying data structure used
135 the Python project. The correlation is as follows:
136
137 .. currentmodule:: sortedcontainers
138
139 ====================== ==================================
140 Data Structure Project
141 ====================== ==================================
142 :class:`SortedList` :doc:`Sorted Containers<index>`
143 :class:`SortedKeyList` :doc:`Sorted Containers<index>`
144 B-Tree `blist on PyPI`_
145 List `sortedcollection recipe`_
146 AVL-Tree `bintrees on PyPI`_
147 RB-Tree `banyan on PyPI`_
148 Skip-List `skiplistcollections on PyPI`_
149 std::map `sortedmap on PyPI`_
150 Treap `treap on PyPI`_
151 ====================== ==================================
152
153 .. _`B-Tree`: https://en.wikipedia.org/wiki/B-tree
154 .. _`blist on PyPI`: https://pypi.org/project/blist/
155 .. _`bintrees on PyPI`: https://pypi.org/project/bintrees/
156 .. _`sortedmap on PyPI`: https://pypi.org/project/sortedmap/
157 .. _`sorteddict on PyPI`: https://pypi.org/project/sorteddict/
158 .. _`pyskiplist on PyPI`: https://pypi.org/project/pyskiplist/
159 .. _`banyan on PyPI`: https://pypi.org/project/Banyan/
160 .. _`treap on PyPI`: https://pypi.org/project/treap/
161 .. _`skiplistcollections on PyPI`: https://pypi.org/project/skiplistcollections/
162 .. _`sortedcollection recipe`: http://code.activestate.com/recipes/577197-sortedcollection/
163 .. _`rbtree on PyPI`: https://pypi.org/project/rbtree/
164 .. _`ruamel.ordereddict on PyPI`: https://pypi.org/project/ruamel.ordereddict/
165 .. _`rbtree from NewCenturyComputers`: http://newcenturycomputers.net/projects/rbtree.html
166 .. _`python-avl-tree from GitHub user pgrafov`: https://github.com/pgrafov/python-avl-tree
167 .. _`pyavl on PyPI`: https://pypi.org/project/pyavl/
168 .. _`skiplist from Bitbucket user mojaves`: https://bitbucket.org/mojaves/pyskiplist/
169 .. _`tree implementation`: https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/ext/pb_ds/tree_based_containers.html
170
171 Sorted List
172 -----------
173
174 Graphs comparing :doc:`sortedlist` performance.
175
176 __init__
177 ........
178
179 Initializing with a list of random numbers using :func:`SortedList.__init__`.
180
181 .. image:: _static/SortedList-init.png
182
183 add
184 ...
185
186 Randomly adding values using :func:`SortedList.add`.
187
188 .. image:: _static/SortedList-add.png
189
190 contains
191 ........
192
193 Randomly testing membership using :func:`SortedList.__contains__`.
194
195 .. image:: _static/SortedList-contains.png
196
197 count
198 .....
199
200 Counting objects at random using :func:`SortedList.count`.
201
202 .. image:: _static/SortedList-count.png
203
204 __delitem__
205 ...........
206
207 Deleting objects at random using :func:`SortedList.__delitem__`.
208
209 .. image:: _static/SortedList-delitem.png
210
211 __getitem__
212 ...........
213
214 Retrieving objects by index using :func:`SortedList.__getitem__`.
215
216 .. image:: _static/SortedList-getitem.png
217
218 index
219 .....
220
221 Finding the index of an object using :func:`SortedList.index`.
222
223 .. image:: _static/SortedList-index.png
224
225 iter
226 ....
227
228 Iterating a SortedList using :func:`SortedList.__iter__`.
229
230 .. image:: _static/SortedList-iter.png
231
232 pop
233 ...
234
235 Removing the last object using :func:`SortedList.pop`.
236
237 .. image:: _static/SortedList-pop.png
238
239 remove
240 ......
241
242 Remove an object at random using :func:`SortedList.remove`.
243
244 .. image:: _static/SortedList-remove.png
245
246 update_large
247 ............
248
249 Updating a SortedList with a large iterable using :func:`SortedList.update`.
250
251 .. image:: _static/SortedList-update_large.png
252
253 update_small
254 ............
255
256 Updating a SortedList with a small iterable using :func:`SortedList.update`.
257
258 .. image:: _static/SortedList-update_small.png
259
260 Sorted Dict
261 -----------
262
263 Graphs comparing :doc:`sorteddict` performance.
264
265 __init__
266 ........
267
268 Initializing with a list of pairs of random numbers using
269 :func:`SortedDict.__init__`.
270
271 .. image:: _static/SortedDict-init.png
272
273 __contains__
274 ............
275
276 Given a key at random, test whether the key is in the dictionary using
277 :func:`SortedDict.__contains__`.
278
279 .. image:: _static/SortedDict-contains.png
280
281 __getitem__
282 ...........
283
284 Given a key at random, retrieve the value using :func:`SortedDict.__getitem__`.
285
286 .. image:: _static/SortedDict-getitem.png
287
288 __setitem__
289 ...........
290
291 Given a key at random, set the value using :func:`SortedDict.__setitem__`.
292
293 .. image:: _static/SortedDict-setitem.png
294
295 __delitem__
296 ...........
297
298 Given a key at random, delete the value using :func:`SortedDict.__delitem__`.
299
300 .. image:: _static/SortedDict-delitem.png
301
302 iter
303 ....
304
305 Iterate the keys of a SortedDict using :func:`SortedDict.__iter__`.
306
307 .. image:: _static/SortedDict-iter.png
308
309 setitem_existing
310 ................
311
312 Given an existing key at random, set the value using
313 :func:`SortedDict.__setitem__`.
314
315 .. image:: _static/SortedDict-setitem_existing.png
316
317 Sorted Set
318 ----------
319
320 Graphs comparing :doc:`sortedset` performance.
321
322 __init__
323 ........
324
325 Initializing with a list of random numbers using :func:`SortedSet.__init__`.
326
327 .. image:: _static/SortedSet-init.png
328
329 add
330 ...
331
332 Randomly add values using :func:`SortedSet.add`.
333
334 .. image:: _static/SortedSet-add.png
335
336 contains
337 ........
338
339 Randomly test membership using :func:`SortedSet.__contains__`.
340
341 .. image:: _static/SortedSet-contains.png
342
343 difference_large
344 ................
345
346 Set difference using :func:`SortedSet.difference`.
347
348 .. image:: _static/SortedSet-difference_large.png
349
350 difference_medium
351 .................
352
353 Set difference using :func:`SortedSet.difference`.
354
355 .. image:: _static/SortedSet-difference_medium.png
356
357 difference_small
358 ................
359
360 Set difference using :func:`SortedSet.difference`.
361
362 .. image:: _static/SortedSet-difference_small.png
363
364 difference_tiny
365 ...............
366
367 Set difference using :func:`SortedSet.difference`.
368
369 .. image:: _static/SortedSet-difference_tiny.png
370
371 difference_update_large
372 .......................
373
374 Set difference using :func:`SortedSet.difference_update`.
375
376 .. image:: _static/SortedSet-difference_update_large.png
377
378 difference_update_medium
379 ........................
380
381 Set difference using :func:`SortedSet.difference_update`.
382
383 .. image:: _static/SortedSet-difference_update_medium.png
384
385 difference_update_small
386 .......................
387
388 Set difference using :func:`SortedSet.difference_update`.
389
390 .. image:: _static/SortedSet-difference_update_small.png
391
392 difference_update_tiny
393 ......................
394
395 Set difference using :func:`SortedSet.difference_update`.
396
397 .. image:: _static/SortedSet-difference_update_tiny.png
398
399 intersection_large
400 ..................
401
402 Set intersection using :func:`SortedSet.intersection`.
403
404 .. image:: _static/SortedSet-intersection_large.png
405
406 intersection_medium
407 ...................
408
409 Set intersection using :func:`SortedSet.intersection`.
410
411 .. image:: _static/SortedSet-intersection_medium.png
412
413 intersection_small
414 ..................
415
416 Set intersection using :func:`SortedSet.intersection`.
417
418 .. image:: _static/SortedSet-intersection_small.png
419
420 intersection_tiny
421 .................
422
423 Set intersection using :func:`SortedSet.intersection`.
424
425 .. image:: _static/SortedSet-intersection_tiny.png
426
427 intersection_update_large
428 .........................
429
430 Set intersection using :func:`SortedSet.intersection_update`.
431
432 .. image:: _static/SortedSet-intersection_update_large.png
433
434 intersection_update_medium
435 ..........................
436
437 Set intersection using :func:`SortedSet.intersection_update`.
438
439 .. image:: _static/SortedSet-intersection_update_medium.png
440
441 intersection_update_small
442 .........................
443
444 Set intersection using :func:`SortedSet.intersection_update`.
445
446 .. image:: _static/SortedSet-intersection_update_small.png
447
448 intersection_update_tiny
449 ........................
450
451 Set intersection using :func:`SortedSet.intersection_update`.
452
453 .. image:: _static/SortedSet-intersection_update_tiny.png
454
455 iter
456 ....
457
458 Iterating a set using :func:`SortedSet.__iter__`.
459
460 .. image:: _static/SortedSet-iter.png
461
462 pop
463 ...
464
465 Remove the last item in a set using :func:`SortedSet.pop`.
466
467 .. image:: _static/SortedSet-pop.png
468
469 remove
470 ......
471
472 Remove an item at random using :func:`SortedSet.remove`.
473
474 .. image:: _static/SortedSet-remove.png
475
476 union_large
477 ...........
478
479 Set union using :func:`SortedSet.union`.
480
481 .. image:: _static/SortedSet-union_large.png
482
483 union_medium
484 ............
485
486 Set union using :func:`SortedSet.union`.
487
488 .. image:: _static/SortedSet-union_medium.png
489
490 union_small
491 ...........
492
493 Set union using :func:`SortedSet.union`.
494
495 .. image:: _static/SortedSet-union_small.png
496
497 union_tiny
498 ..........
499
500 Set union using :func:`SortedSet.union`.
501
502 .. image:: _static/SortedSet-union_tiny.png
503
504 update_large
505 ............
506
507 Set update using :func:`SortedSet.update`.
508
509 .. image:: _static/SortedSet-update_large.png
510
511 update_medium
512 .............
513
514 Set update using :func:`SortedSet.update`.
515
516 .. image:: _static/SortedSet-update_medium.png
517
518 update_small
519 ............
520
521 Set update using :func:`SortedSet.update`.
522
523 .. image:: _static/SortedSet-update_small.png
524
525 update_tiny
526 ...........
527
528 Set update using :func:`SortedSet.update`.
529
530 .. image:: _static/SortedSet-update_tiny.png
531
532 symmetric_difference_large
533 ..........................
534
535 Set symmetric-difference using :func:`SortedSet.symmetric_difference`.
536
537 .. image:: _static/SortedSet-symmetric_difference_large.png
538
539 symmetric_difference_medium
540 ...........................
541
542 Set symmetric-difference using :func:`SortedSet.symmetric_difference`.
543
544 .. image:: _static/SortedSet-symmetric_difference_medium.png
545
546 symmetric_difference_small
547 ..........................
548
549 Set symmetric-difference using :func:`SortedSet.symmetric_difference`.
550
551 .. image:: _static/SortedSet-symmetric_difference_small.png
552
553 symmetric_difference_tiny
554 .........................
555
556 Set symmetric-difference using :func:`SortedSet.symmetric_difference`.
557
558 .. image:: _static/SortedSet-symmetric_difference_tiny.png
559
560 symm_diff_update_large
561 ......................
562
563 Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`.
564
565 .. image:: _static/SortedSet-symmetric_difference_update_large.png
566
567 symm_diff_update_medium
568 .......................
569
570 Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`.
571
572 .. image:: _static/SortedSet-symmetric_difference_update_medium.png
573
574 symm_diff_update_small
575 ......................
576
577 Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`.
578
579 .. image:: _static/SortedSet-symmetric_difference_update_small.png
580
581 symm_diff_update_tiny
582 .....................
583
584 Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`.
585
586 .. image:: _static/SortedSet-symmetric_difference_update_tiny.png