| 1 |
Simulated Workload Performance Comparison |
| 2 |
========================================= |
| 3 |
|
| 4 |
While the :doc:`competitive performance comparison<performance>` is useful for |
| 5 |
identifying strengths and weaknesses in the API, it doesn't reflect the |
| 6 |
variable nature of real-life workloads. In August 2014, a survey of use cases |
| 7 |
for sorted list data types was made through repeated searches of Github. The |
| 8 |
analysis identified several common use patterns. |
| 9 |
|
| 10 |
These benchmarks attempt to mimic the usage patterns observed in other |
| 11 |
projects. Those patterns are summarized in the following names: |
| 12 |
|
| 13 |
* *Priority Queue* -- Represents a priority queue data structure. In addition |
| 14 |
to the normal `add` and `pop` routines that can be efficiently managed with |
| 15 |
the `heapq` module in the standard library, projects required the ability to |
| 16 |
test for list ownership of values and occasional removal. A common use case |
| 17 |
also involved iterating the entire priority queue. The recipe described in |
| 18 |
the Python docs is insufficient as a priority queue for these projects in two |
| 19 |
ways: not every inserted value corresponds to a unique key and sorted |
| 20 |
iteration is not supported in linear time. |
| 21 |
|
| 22 |
* *Multiset* -- Represents a multiset data structure. While the |
| 23 |
`collections.Counter` data type is commonly suggested for implementing a |
| 24 |
multiset in Python, some projects required the ability to efficiently lookup |
| 25 |
the greatest or least item in the set. |
| 26 |
|
| 27 |
* *Ranking* -- Represents those projects that repeatedly looked up the index of |
| 28 |
items in the sorted list. Sometimes this occurred as part of prioritizing |
| 29 |
elements and reporting their rank. Imagine trying to identify an element's |
| 30 |
position in a work queue. |
| 31 |
|
| 32 |
* *Neighbor* -- This pattern was observed in several implementations of |
| 33 |
machine-learning algorithms (e.g. K-nearest-neighbor search). An iterative |
| 34 |
process was applied to a set of values in which they were repeatedly bisected |
| 35 |
and occasionally iterated. The bisect process attempts to find the nearest |
| 36 |
values to a given one. |
| 37 |
|
| 38 |
* *Intervals* -- Perhaps the most complex usage pattern, these projects |
| 39 |
maintained a list of intervals and were often querying a sorted list to |
| 40 |
determine those which overlapped. In addition to bisecting the list to |
| 41 |
identify nearest intervals, range queries were used to determine |
| 42 |
overlap. Frequent indexing was also apparent. |
| 43 |
|
| 44 |
The performance of each benchmark is displayed below along with each of the |
| 45 |
other data types implementing the necessary operations. Before the benchmark |
| 46 |
begins, the list is populated with values and then corresponding to the list |
| 47 |
size, a number of operations are performed. Each workload has been reduced to a |
| 48 |
set of operations with associated frequencies. For example, the priority queue |
| 49 |
simulates `add` and `pop` each 40% of the time. |
| 50 |
|
| 51 |
Though these workloads strive to be realistic, they are still quite |
| 52 |
synthetic. No attempt is made to exercise memory allocation or cache |
| 53 |
interference while sorted list operations are performed. The frequency of each |
| 54 |
operation is also estimated because no projects had performance benchmarks that |
| 55 |
were easily evaluated. |
| 56 |
|
| 57 |
The legends of the graphs below correlate the underlying data structure used to |
| 58 |
the Python project. The correlation is as follows: |
| 59 |
|
| 60 |
.. currentmodule:: sortedcontainers |
| 61 |
|
| 62 |
====================== ================================== |
| 63 |
Data Structure Project |
| 64 |
====================== ================================== |
| 65 |
:class:`SortedList` :doc:`Sorted Containers<index>` |
| 66 |
:class:`SortedKeyList` :doc:`Sorted Containers<index>` |
| 67 |
B-Tree `blist on PyPI`_ |
| 68 |
List `sortedcollection recipe`_ |
| 69 |
====================== ================================== |
| 70 |
|
| 71 |
.. _`blist on PyPI`: https://pypi.org/project/blist/ |
| 72 |
.. _`sortedcollection recipe`: http://code.activestate.com/recipes/577197-sortedcollection/ |
| 73 |
|
| 74 |
Sorted List |
| 75 |
----------- |
| 76 |
|
| 77 |
Graphs comparing :doc:`sortedlist` performance. |
| 78 |
|
| 79 |
Priority Queue |
| 80 |
.............. |
| 81 |
|
| 82 |
Simulates a *Priority Queue* workload as described above. The mix of operations |
| 83 |
and their frequencies: |
| 84 |
|
| 85 |
* 40% :func:`SortedList.add` |
| 86 |
* 40% :func:`SortedList.pop` |
| 87 |
* 10% :func:`SortedList.discard` |
| 88 |
* 9% :func:`SortedList.__contains__` |
| 89 |
* 1% :func:`SortedList.__iter__` (limited to first 100 elements) |
| 90 |
|
| 91 |
.. image:: _static/SortedList-priorityqueue.png |
| 92 |
|
| 93 |
.. image:: _static/SortedList_runtime-priorityqueue.png |
| 94 |
|
| 95 |
.. image:: _static/SortedList_load-priorityqueue.png |
| 96 |
|
| 97 |
Multiset |
| 98 |
........ |
| 99 |
|
| 100 |
Simulates a *Multiset* workload as described above. The mix of operations and |
| 101 |
their frequencies: |
| 102 |
|
| 103 |
* 75% :func:`SortedList.__contains__` |
| 104 |
* 10% :func:`SortedList.add` |
| 105 |
* 10% :func:`SortedList.remove` |
| 106 |
* 5% :func:`SortedList.__getitem__` |
| 107 |
|
| 108 |
.. image:: _static/SortedList-multiset.png |
| 109 |
|
| 110 |
.. image:: _static/SortedList_runtime-multiset.png |
| 111 |
|
| 112 |
.. image:: _static/SortedList_load-multiset.png |
| 113 |
|
| 114 |
Ranking |
| 115 |
....... |
| 116 |
|
| 117 |
Simulates a *Ranking* workload as described above. The mix of operations and |
| 118 |
their frequencies: |
| 119 |
|
| 120 |
* 40% :func:`SortedList.__getitem__` |
| 121 |
* 40% :func:`SortedList.index` |
| 122 |
* 10% :func:`SortedList.add` |
| 123 |
* 10% :func:`SortedList.remove` |
| 124 |
|
| 125 |
.. image:: _static/SortedList-ranking.png |
| 126 |
|
| 127 |
.. image:: _static/SortedList_runtime-ranking.png |
| 128 |
|
| 129 |
.. image:: _static/SortedList_load-ranking.png |
| 130 |
|
| 131 |
Neighbor |
| 132 |
........ |
| 133 |
|
| 134 |
Simulates a *Neighbor* workload as described above. The mix of operations and |
| 135 |
their frequencies: |
| 136 |
|
| 137 |
* 75% :func:`SortedList.bisect` |
| 138 |
* 10% :func:`SortedList.add` |
| 139 |
* 10% :func:`SortedList.remove` |
| 140 |
* 5% :func:`SortedList.__iter__` (limited to first 100 elements) |
| 141 |
|
| 142 |
.. image:: _static/SortedList-neighbor.png |
| 143 |
|
| 144 |
.. image:: _static/SortedList_runtime-neighbor.png |
| 145 |
|
| 146 |
.. image:: _static/SortedList_load-neighbor.png |
| 147 |
|
| 148 |
Intervals |
| 149 |
......... |
| 150 |
|
| 151 |
Simulates an *Intervals* workload as described above. The mix of operations and |
| 152 |
their frequencies: |
| 153 |
|
| 154 |
* 30% :func:`SortedList.bisect` |
| 155 |
* 20% :func:`SortedList.__getitem__` |
| 156 |
* 20% :func:`SortedList.__delitem__` |
| 157 |
* 10% :func:`SortedList.__getitem__` (range query) |
| 158 |
* 10% :func:`SortedList.add` |
| 159 |
* 10% :func:`SortedList.discard` |
| 160 |
|
| 161 |
.. image:: _static/SortedList-intervals.png |
| 162 |
|
| 163 |
.. image:: _static/SortedList_runtime-intervals.png |
| 164 |
|
| 165 |
.. image:: _static/SortedList_load-intervals.png |
| 166 |
|
| 167 |
Other Performance Comparisons |
| 168 |
----------------------------- |
| 169 |
|
| 170 |
:doc:`Sorted Containers<index>` uses a segmented-list data structure similar to |
| 171 |
a B-tree limited to two levels of nodes. As part of the implementation, a load |
| 172 |
factor is used to determine how many values should be stored in each node. This |
| 173 |
can have a significant impact on performance and a :doc:`load factor |
| 174 |
performance comparison<performance-load>` is also provided. |
| 175 |
|
| 176 |
Because :doc:`Sorted Containers<index>` is pure-Python, its performance also |
| 177 |
depends directly on the Python runtime. A :doc:`runtime performance |
| 178 |
comparison<performance-runtime>` is also included with data from popular Python |
| 179 |
runtimes. |