| 1 |
"""Count splits that occur in SortedList._expand with varying loads. |
| 2 |
|
| 3 |
NOTE: Benchmarking performance at scale eventually used sampling rather than |
| 4 |
what is described below. Do not use this method for benchmarking. This file is |
| 5 |
kept for SortedListWithSplits which presents an example for recording the |
| 6 |
number of sublist splits that occur. |
| 7 |
|
| 8 |
Compare three load styles: |
| 9 |
|
| 10 |
1. Low - Simply initializing a SortedList creates each sublist with length |
| 11 |
equal to `load`. |
| 12 |
|
| 13 |
2. Normal Random - See `init_sorted_list` below. Each sublist is given a length |
| 14 |
chosen at random from a normal distribution with mean 1500 and standard |
| 15 |
deviation 100. See `plot_lengths_histogram.py` for a visualization of the |
| 16 |
sublist length and motivation for the normal distribution parameters. |
| 17 |
|
| 18 |
3. Added Random - Each sublist is initialized by simply adding random values to |
| 19 |
the list. This one is the most realistic but expensive to compute. It's also |
| 20 |
biased by the number of values added. Because SortedList amortizes some costs, |
| 21 |
this metric is prone to under or over-estimating impact. |
| 22 |
|
| 23 |
Sample output: LIMIT = int(1e6), RATIO = 1 |
| 24 |
|
| 25 |
Low [504, 490, 487, 503, 488, 494, 496, 502, 493, 486] |
| 26 |
Normal Random [666, 667, 662, 666, 670, 668, 668, 666, 668, 666] |
| 27 |
Added Random [685, 675, 689, 687, 699, 706, 691, 679, 672, 702] |
| 28 |
|
| 29 |
Conclusion: |
| 30 |
|
| 31 |
Normal Random is about 1-2% less than Added Random on average but remains |
| 32 |
practical for use as an efficient approximation. |
| 33 |
|
| 34 |
The above values were calculated with RATIO = 1. Increasing the ratio to 10 |
| 35 |
creates a significant divergence between Normal Random and Added Random. Sample |
| 36 |
output with RATIO = 10: |
| 37 |
|
| 38 |
Low [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 39 |
Normal Random [1, 2, 0, 1, 0, 1, 0, 1, 1, 2] |
| 40 |
Added Random [307, 286, 290, 253, 279, 319, 283, 264, 278, 280] |
| 41 |
|
| 42 |
The difference is due to the load of the sublists. Randomly adding a million |
| 43 |
values positions the sublist lengths in a bimodal distribution split at 1000 |
| 44 |
and 2000. This is nearly the worst-case scenario for measurements. |
| 45 |
|
| 46 |
After trying a number of settings, RATIO = 3 appears reliable and useful. This |
| 47 |
approximates the available capacity: |
| 48 |
|
| 49 |
>>> random.seed(0) |
| 50 |
>>> sl = init_sorted_list(sc.SortedList(), int(1e6)) |
| 51 |
>>> len(sl._lists) * sl._load * 2 - len(sl) |
| 52 |
334000 |
| 53 |
|
| 54 |
So after constructing a SortedList with sublist length given by a normal |
| 55 |
distribution with mean 1,500 and standard deviation 100, we fill the remaining |
| 56 |
empty capacity. |
| 57 |
|
| 58 |
""" |
| 59 |
|
| 60 |
from __future__ import print_function |
| 61 |
|
| 62 |
import sortedcontainers as sc |
| 63 |
import random |
| 64 |
|
| 65 |
REPEAT = 5 |
| 66 |
LIMIT = int(1e6) |
| 67 |
RATIO = 3 |
| 68 |
|
| 69 |
|
| 70 |
class SortedListWithSplits(sc.SortedList): |
| 71 |
"SortedList that counts splits that occur in _expand." |
| 72 |
|
| 73 |
def __init__(self, *args, **kwargs): |
| 74 |
self.splits = 0 |
| 75 |
super(SortedListWithSplits, self).__init__(*args, **kwargs) |
| 76 |
|
| 77 |
def _expand(self, pos): |
| 78 |
if len(self._lists[pos]) > self._twice: |
| 79 |
self.splits += 1 |
| 80 |
super(SortedListWithSplits, self)._expand(pos) |
| 81 |
|
| 82 |
|
| 83 |
def init_sorted_list(sl, size): |
| 84 |
"Initialize a SortedList with normally distributed sublist lengths." |
| 85 |
sl.clear() |
| 86 |
|
| 87 |
total = 0 |
| 88 |
|
| 89 |
while total < size: |
| 90 |
# count = random.randrange(sl._load, sl._twice) |
| 91 |
count = int(random.normalvariate(int(sl._load * 1.5), 100)) |
| 92 |
count = min(count, sl._load * 2) |
| 93 |
count = max(count, sl._load) |
| 94 |
sl._lists.append(list(range(total, total + count))) |
| 95 |
total += count |
| 96 |
|
| 97 |
sl._len = sum(len(sublist) for sublist in sl._lists) |
| 98 |
sl._maxes[:] = [sublist[-1] for sublist in sl._lists] |
| 99 |
|
| 100 |
sl._check() |
| 101 |
|
| 102 |
while len(sl) > size: |
| 103 |
del sl[random.randrange(len(sl))] |
| 104 |
|
| 105 |
return sl |
| 106 |
|
| 107 |
|
| 108 |
def fill(obj, count, limit): |
| 109 |
"Repeatedly add random values to the SortedList." |
| 110 |
for each in range(count): |
| 111 |
obj.add(random.randrange(limit)) |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == '__main__': |
| 115 |
low_splits = [] |
| 116 |
|
| 117 |
for each in range(REPEAT): |
| 118 |
sl = SortedListWithSplits(range(LIMIT)) |
| 119 |
fill(sl, LIMIT / RATIO, LIMIT) |
| 120 |
low_splits.append(sl.splits) |
| 121 |
|
| 122 |
uniform_splits = [] |
| 123 |
|
| 124 |
for each in range(REPEAT): |
| 125 |
sl = init_sorted_list(SortedListWithSplits(), LIMIT) |
| 126 |
fill(sl, LIMIT / RATIO, LIMIT) |
| 127 |
uniform_splits.append(sl.splits) |
| 128 |
|
| 129 |
rand_splits = [] |
| 130 |
|
| 131 |
for each in range(REPEAT): |
| 132 |
sl = SortedListWithSplits() |
| 133 |
|
| 134 |
values = list(range(LIMIT)) |
| 135 |
random.shuffle(values) |
| 136 |
for value in values: |
| 137 |
sl.add(value) |
| 138 |
|
| 139 |
sl.splits = 0 |
| 140 |
|
| 141 |
fill(sl, LIMIT / RATIO, LIMIT) |
| 142 |
rand_splits.append(sl.splits) |
| 143 |
|
| 144 |
print('Low', low_splits) |
| 145 |
print('Normal Random', uniform_splits) |
| 146 |
print('Added Random', rand_splits) |