| 1 |
Load Factor Performance Comparison |
| 2 |
================================== |
| 3 |
|
| 4 |
:doc:`Sorted Containers<index>` uses a segmented-list data structure similar to |
| 5 |
a B-tree limited to two levels of nodes. As part of the implementation, a load |
| 6 |
factor is used to determine how many values should be stored in each node. This |
| 7 |
page compares three load factors on containers with as many as ten of million |
| 8 |
elements. |
| 9 |
|
| 10 |
No single load factor is universally superior. The best load factor for your |
| 11 |
purposes will depend on your usage pattern. Originally, :doc:`Sorted |
| 12 |
Containers<index>` used a load factor of 100 but that changed in release 0.8.5 |
| 13 |
to 1,000 due to the :ref:`SortedList.__delitem__<SortedList.delitem>` benchmark |
| 14 |
which was dramatically impacted. Most benchmarks perform slightly better with a |
| 15 |
load factor of 100 but each is competitive with alternate loads. For an |
| 16 |
in-depth analysis of the load factor read :doc:`Performance at |
| 17 |
Scale<performance-scale>`. |
| 18 |
|
| 19 |
Performance of competing implementations are benchmarked against the CPython |
| 20 |
3.7 runtime. An :doc:`implementation performance comparison<performance>` is |
| 21 |
also included with data from popular sorted collections packages. |
| 22 |
|
| 23 |
Because :doc:`Sorted Containers<index>` is pure-Python, its performance also |
| 24 |
depends directly on the Python runtime. A :doc:`runtime performance |
| 25 |
comparison<performance-runtime>` is also included with data from popular Python |
| 26 |
runtimes. |
| 27 |
|
| 28 |
Though these benchmarks exercise only one API repeatedly, an effort has also |
| 29 |
been made to simulate real-world workloads. The :doc:`simulated workload |
| 30 |
performance comparison<performance-workload>` contains examples with |
| 31 |
comparisons to other implementations, load factors, and runtimes. |
| 32 |
|
| 33 |
.. currentmodule:: sortedcontainers |
| 34 |
|
| 35 |
Sorted List |
| 36 |
----------- |
| 37 |
|
| 38 |
Graphs comparing :doc:`sortedlist` performance. |
| 39 |
|
| 40 |
__init__ |
| 41 |
........ |
| 42 |
|
| 43 |
Initializing with a list of random numbers using :func:`SortedList.__init__`. |
| 44 |
|
| 45 |
.. image:: _static/SortedList_load-init.png |
| 46 |
|
| 47 |
add |
| 48 |
... |
| 49 |
|
| 50 |
Randomly adding values using :func:`SortedList.add`. |
| 51 |
|
| 52 |
.. image:: _static/SortedList_load-add.png |
| 53 |
|
| 54 |
contains |
| 55 |
........ |
| 56 |
|
| 57 |
Randomly testing membership using :func:`SortedList.__contains__`. |
| 58 |
|
| 59 |
.. image:: _static/SortedList_load-contains.png |
| 60 |
|
| 61 |
count |
| 62 |
..... |
| 63 |
|
| 64 |
Counting objects at random using :func:`SortedList.count`. |
| 65 |
|
| 66 |
.. image:: _static/SortedList_load-count.png |
| 67 |
|
| 68 |
__delitem__ |
| 69 |
........... |
| 70 |
|
| 71 |
.. _SortedList.delitem: |
| 72 |
|
| 73 |
Deleting objects at random using :func:`SortedList.__delitem__`. |
| 74 |
|
| 75 |
.. image:: _static/SortedList_load-delitem.png |
| 76 |
|
| 77 |
__getitem__ |
| 78 |
........... |
| 79 |
|
| 80 |
Retrieving objects by index using :func:`SortedList.__getitem__`. |
| 81 |
|
| 82 |
.. image:: _static/SortedList_load-getitem.png |
| 83 |
|
| 84 |
index |
| 85 |
..... |
| 86 |
|
| 87 |
Finding the index of an object using :func:`SortedList.index`. |
| 88 |
|
| 89 |
.. image:: _static/SortedList_load-index.png |
| 90 |
|
| 91 |
iter |
| 92 |
.... |
| 93 |
|
| 94 |
Iterating a SortedList using :func:`SortedList.__iter__`. |
| 95 |
|
| 96 |
.. image:: _static/SortedList_load-iter.png |
| 97 |
|
| 98 |
pop |
| 99 |
... |
| 100 |
|
| 101 |
Removing the last object using :func:`SortedList.pop`. |
| 102 |
|
| 103 |
.. image:: _static/SortedList_load-pop.png |
| 104 |
|
| 105 |
remove |
| 106 |
...... |
| 107 |
|
| 108 |
Remove an object at random using :func:`SortedList.remove`. |
| 109 |
|
| 110 |
.. image:: _static/SortedList_load-remove.png |
| 111 |
|
| 112 |
update_large |
| 113 |
............ |
| 114 |
|
| 115 |
Updating a SortedList with a large iterable using :func:`SortedList.update`. |
| 116 |
|
| 117 |
.. image:: _static/SortedList_load-update_large.png |
| 118 |
|
| 119 |
update_small |
| 120 |
............ |
| 121 |
|
| 122 |
Updating a SortedList with a small iterable using :func:`SortedList.update`. |
| 123 |
|
| 124 |
.. image:: _static/SortedList_load-update_small.png |
| 125 |
|
| 126 |
Sorted Dict |
| 127 |
----------- |
| 128 |
|
| 129 |
Graphs comparing :doc:`sorteddict` performance. |
| 130 |
|
| 131 |
__init__ |
| 132 |
........ |
| 133 |
|
| 134 |
Initializing with a list of pairs of random numbers using |
| 135 |
:func:`SortedDict.__init__`. |
| 136 |
|
| 137 |
.. image:: _static/SortedDict_load-init.png |
| 138 |
|
| 139 |
__contains__ |
| 140 |
............ |
| 141 |
|
| 142 |
Given a key at random, test whether the key is in the dictionary using |
| 143 |
:func:`SortedDict.__contains__`. |
| 144 |
|
| 145 |
.. image:: _static/SortedDict_load-contains.png |
| 146 |
|
| 147 |
__getitem__ |
| 148 |
........... |
| 149 |
|
| 150 |
Given a key at random, retrieve the value using :func:`SortedDict.__getitem__`. |
| 151 |
|
| 152 |
.. image:: _static/SortedDict_load-getitem.png |
| 153 |
|
| 154 |
__setitem__ |
| 155 |
........... |
| 156 |
|
| 157 |
Given a key at random, set the value using :func:`SortedDict.__setitem__`. |
| 158 |
|
| 159 |
.. image:: _static/SortedDict_load-setitem.png |
| 160 |
|
| 161 |
__delitem__ |
| 162 |
........... |
| 163 |
|
| 164 |
Given a key at random, delete the value using :func:`SortedDict.__delitem__`. |
| 165 |
|
| 166 |
.. image:: _static/SortedDict_load-delitem.png |
| 167 |
|
| 168 |
iter |
| 169 |
.... |
| 170 |
|
| 171 |
Iterate the keys of a SortedDict using :func:`SortedDict.__iter__`. |
| 172 |
|
| 173 |
.. image:: _static/SortedDict_load-iter.png |
| 174 |
|
| 175 |
setitem_existing |
| 176 |
................ |
| 177 |
|
| 178 |
Given an existing key at random, set the value using |
| 179 |
:func:`SortedDict.__setitem__`. |
| 180 |
|
| 181 |
.. image:: _static/SortedDict_load-setitem_existing.png |
| 182 |
|
| 183 |
Sorted Set |
| 184 |
---------- |
| 185 |
|
| 186 |
Graphs comparing :doc:`sortedset` performance. |
| 187 |
|
| 188 |
__init__ |
| 189 |
........ |
| 190 |
|
| 191 |
Initializing with a list of random numbers using :func:`SortedSet.__init__`. |
| 192 |
|
| 193 |
.. image:: _static/SortedSet_load-init.png |
| 194 |
|
| 195 |
add |
| 196 |
... |
| 197 |
|
| 198 |
Randomly add values using :func:`SortedSet.add`. |
| 199 |
|
| 200 |
.. image:: _static/SortedSet_load-add.png |
| 201 |
|
| 202 |
contains |
| 203 |
........ |
| 204 |
|
| 205 |
Randomly test membership using :func:`SortedSet.__contains__`. |
| 206 |
|
| 207 |
.. image:: _static/SortedSet_load-contains.png |
| 208 |
|
| 209 |
difference_large |
| 210 |
................ |
| 211 |
|
| 212 |
Set difference using :func:`SortedSet.difference`. |
| 213 |
|
| 214 |
.. image:: _static/SortedSet_load-difference_large.png |
| 215 |
|
| 216 |
difference_medium |
| 217 |
................. |
| 218 |
|
| 219 |
Set difference using :func:`SortedSet.difference`. |
| 220 |
|
| 221 |
.. image:: _static/SortedSet_load-difference_medium.png |
| 222 |
|
| 223 |
difference_small |
| 224 |
................ |
| 225 |
|
| 226 |
Set difference using :func:`SortedSet.difference`. |
| 227 |
|
| 228 |
.. image:: _static/SortedSet_load-difference_small.png |
| 229 |
|
| 230 |
difference_tiny |
| 231 |
............... |
| 232 |
|
| 233 |
Set difference using :func:`SortedSet.difference`. |
| 234 |
|
| 235 |
.. image:: _static/SortedSet_load-difference_tiny.png |
| 236 |
|
| 237 |
difference_update_large |
| 238 |
....................... |
| 239 |
|
| 240 |
Set difference using :func:`SortedSet.difference_update`. |
| 241 |
|
| 242 |
.. image:: _static/SortedSet_load-difference_update_large.png |
| 243 |
|
| 244 |
difference_update_medium |
| 245 |
........................ |
| 246 |
|
| 247 |
Set difference using :func:`SortedSet.difference_update`. |
| 248 |
|
| 249 |
.. image:: _static/SortedSet_load-difference_update_medium.png |
| 250 |
|
| 251 |
difference_update_small |
| 252 |
....................... |
| 253 |
|
| 254 |
Set difference using :func:`SortedSet.difference_update`. |
| 255 |
|
| 256 |
.. image:: _static/SortedSet_load-difference_update_small.png |
| 257 |
|
| 258 |
difference_update_tiny |
| 259 |
...................... |
| 260 |
|
| 261 |
Set difference using :func:`SortedSet.difference_update`. |
| 262 |
|
| 263 |
.. image:: _static/SortedSet_load-difference_update_tiny.png |
| 264 |
|
| 265 |
intersection_large |
| 266 |
.................. |
| 267 |
|
| 268 |
Set intersection using :func:`SortedSet.intersection`. |
| 269 |
|
| 270 |
.. image:: _static/SortedSet_load-intersection_large.png |
| 271 |
|
| 272 |
intersection_medium |
| 273 |
................... |
| 274 |
|
| 275 |
Set intersection using :func:`SortedSet.intersection`. |
| 276 |
|
| 277 |
.. image:: _static/SortedSet_load-intersection_medium.png |
| 278 |
|
| 279 |
intersection_small |
| 280 |
.................. |
| 281 |
|
| 282 |
Set intersection using :func:`SortedSet.intersection`. |
| 283 |
|
| 284 |
.. image:: _static/SortedSet_load-intersection_small.png |
| 285 |
|
| 286 |
intersection_tiny |
| 287 |
................. |
| 288 |
|
| 289 |
Set intersection using :func:`SortedSet.intersection`. |
| 290 |
|
| 291 |
.. image:: _static/SortedSet_load-intersection_tiny.png |
| 292 |
|
| 293 |
intersection_update_large |
| 294 |
......................... |
| 295 |
|
| 296 |
Set intersection using :func:`SortedSet.intersection_update`. |
| 297 |
|
| 298 |
.. image:: _static/SortedSet_load-intersection_update_large.png |
| 299 |
|
| 300 |
intersection_update_medium |
| 301 |
.......................... |
| 302 |
|
| 303 |
Set intersection using :func:`SortedSet.intersection_update`. |
| 304 |
|
| 305 |
.. image:: _static/SortedSet_load-intersection_update_medium.png |
| 306 |
|
| 307 |
intersection_update_small |
| 308 |
......................... |
| 309 |
|
| 310 |
Set intersection using :func:`SortedSet.intersection_update`. |
| 311 |
|
| 312 |
.. image:: _static/SortedSet_load-intersection_update_small.png |
| 313 |
|
| 314 |
intersection_update_tiny |
| 315 |
........................ |
| 316 |
|
| 317 |
Set intersection using :func:`SortedSet.intersection_update`. |
| 318 |
|
| 319 |
.. image:: _static/SortedSet_load-intersection_update_tiny.png |
| 320 |
|
| 321 |
iter |
| 322 |
.... |
| 323 |
|
| 324 |
Iterating a set using :func:`iter(SortedSet)`. |
| 325 |
|
| 326 |
.. image:: _static/SortedSet_load-iter.png |
| 327 |
|
| 328 |
pop |
| 329 |
... |
| 330 |
|
| 331 |
Remove the last item in a set using :func:`SortedSet.pop`. |
| 332 |
|
| 333 |
.. image:: _static/SortedSet_load-pop.png |
| 334 |
|
| 335 |
remove |
| 336 |
...... |
| 337 |
|
| 338 |
Remove an item at random using :func:`SortedSet.remove`. |
| 339 |
|
| 340 |
.. image:: _static/SortedSet_load-remove.png |
| 341 |
|
| 342 |
union_large |
| 343 |
........... |
| 344 |
|
| 345 |
Set union using :func:`SortedSet.union`. |
| 346 |
|
| 347 |
.. image:: _static/SortedSet_load-union_large.png |
| 348 |
|
| 349 |
union_medium |
| 350 |
............ |
| 351 |
|
| 352 |
Set union using :func:`SortedSet.union`. |
| 353 |
|
| 354 |
.. image:: _static/SortedSet_load-union_medium.png |
| 355 |
|
| 356 |
union_small |
| 357 |
........... |
| 358 |
|
| 359 |
Set union using :func:`SortedSet.union`. |
| 360 |
|
| 361 |
.. image:: _static/SortedSet_load-union_small.png |
| 362 |
|
| 363 |
union_tiny |
| 364 |
.......... |
| 365 |
|
| 366 |
Set union using :func:`SortedSet.union`. |
| 367 |
|
| 368 |
.. image:: _static/SortedSet_load-union_tiny.png |
| 369 |
|
| 370 |
update_large |
| 371 |
............ |
| 372 |
|
| 373 |
Set update using :func:`SortedSet.update`. |
| 374 |
|
| 375 |
.. image:: _static/SortedSet_load-update_large.png |
| 376 |
|
| 377 |
update_medium |
| 378 |
............. |
| 379 |
|
| 380 |
Set update using :func:`SortedSet.update`. |
| 381 |
|
| 382 |
.. image:: _static/SortedSet_load-update_medium.png |
| 383 |
|
| 384 |
update_small |
| 385 |
............ |
| 386 |
|
| 387 |
Set update using :func:`SortedSet.update`. |
| 388 |
|
| 389 |
.. image:: _static/SortedSet_load-update_small.png |
| 390 |
|
| 391 |
update_tiny |
| 392 |
........... |
| 393 |
|
| 394 |
Set update using :func:`SortedSet.update`. |
| 395 |
|
| 396 |
.. image:: _static/SortedSet_load-update_tiny.png |
| 397 |
|
| 398 |
symmetric_difference_large |
| 399 |
.......................... |
| 400 |
|
| 401 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference`. |
| 402 |
|
| 403 |
.. image:: _static/SortedSet_load-symmetric_difference_large.png |
| 404 |
|
| 405 |
symmetric_difference_medium |
| 406 |
........................... |
| 407 |
|
| 408 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference`. |
| 409 |
|
| 410 |
.. image:: _static/SortedSet_load-symmetric_difference_medium.png |
| 411 |
|
| 412 |
symmetric_difference_small |
| 413 |
.......................... |
| 414 |
|
| 415 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference`. |
| 416 |
|
| 417 |
.. image:: _static/SortedSet_load-symmetric_difference_small.png |
| 418 |
|
| 419 |
symmetric_difference_tiny |
| 420 |
......................... |
| 421 |
|
| 422 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference`. |
| 423 |
|
| 424 |
.. image:: _static/SortedSet_load-symmetric_difference_tiny.png |
| 425 |
|
| 426 |
symm_diff_update_large |
| 427 |
...................... |
| 428 |
|
| 429 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`. |
| 430 |
|
| 431 |
.. image:: _static/SortedSet_load-symmetric_difference_update_large.png |
| 432 |
|
| 433 |
symm_diff_update_medium |
| 434 |
....................... |
| 435 |
|
| 436 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`. |
| 437 |
|
| 438 |
.. image:: _static/SortedSet_load-symmetric_difference_update_medium.png |
| 439 |
|
| 440 |
symm_diff_update_small |
| 441 |
...................... |
| 442 |
|
| 443 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`. |
| 444 |
|
| 445 |
.. image:: _static/SortedSet_load-symmetric_difference_update_small.png |
| 446 |
|
| 447 |
symm_diff_update_tiny |
| 448 |
..................... |
| 449 |
|
| 450 |
Set symmetric-difference using :func:`SortedSet.symmetric_difference_update`. |
| 451 |
|
| 452 |
.. image:: _static/SortedSet_load-symmetric_difference_update_tiny.png |