forked from facebookincubator/ft_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_bench.py
102 lines (83 loc) · 3.24 KB
/
list_bench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright (c) Meta Platforms, Inc. and affiliates.
# pyre-strict
from typing import Any
from ft_utils.benchmark_utils import BenchmarkProvider, execute_benchmarks, ft_randint
from ft_utils.local import LocalWrapper
class ListBenchmarkProvider(BenchmarkProvider):
def __init__(self, operations: int) -> None:
self._operations = operations
self._int_list: list[int] = list(range(1024))
ll = []
for _ in range(1024):
ll.append(ll)
self._ref_list: list[Any] = ll # pyre-ignore[4]
def benchmark_random_read_int(self) -> None:
lst = LocalWrapper(self._int_list)
num_operations = self._operations
lsz = len(lst)
for _ in range(num_operations):
_ = lst[ft_randint(0, lsz - 1)]
def benchmark_random_read_ref(self) -> None:
lst = LocalWrapper(self._ref_list)
num_operations = self._operations
lsz = len(lst)
for _ in range(num_operations):
_ = lst[ft_randint(0, lsz - 1)][ft_randint(0, lsz - 1)]
def benchmark_random_write_int(self) -> None:
lst = LocalWrapper(self._int_list)
num_operations = self._operations
lsz = len(lst)
for idx in range(num_operations):
lst[ft_randint(0, lsz - 1)] = idx
def benchmark_random_read_write_int(self) -> None:
lst = LocalWrapper(self._int_list)
num_operations = self._operations
lsz = len(lst)
for _ in range(num_operations):
lst[ft_randint(0, lsz - 1)] = lst[ft_randint(0, lsz - 1)]
def benchmark_sequential_read_write_int(self) -> None:
lst = LocalWrapper(self._int_list)
num_operations = self._operations
lsz = len(lst)
for idx in range(num_operations):
pos = idx % lsz
lst[pos] = idx
_ = lst[pos]
def benchmark_resize_int(self) -> None:
lst = LocalWrapper(self._int_list)
num_operations = self._operations
for idx in range(num_operations):
lst.append(idx)
for _ in range(num_operations):
lst.pop()
def benchmark_resize_ref(self) -> None:
lst = LocalWrapper(self._ref_list)
num_operations = self._operations
for idx in range(num_operations):
lst.append(idx)
for _ in range(num_operations):
lst.pop()
def benchmark_concurrent_resize_read_write_int(self) -> None:
self._crrw(self._int_list)
def benchmark_concurrent_resize_read_write_ref(self) -> None:
self._crrw(self._ref_list)
def _crrw(self, lst_in: list[Any]) -> None: # pyre-ignore[2]
lst = LocalWrapper(lst_in)
num_operations = self._operations
for idx in range(num_operations):
try_again = True
while try_again:
try:
pos = idx % len(lst)
lst.append(lst[pos])
lst.pop(0)
pos_a = (37 + idx) % len(lst)
pos_b = idx % len(lst)
lst[pos_a] = lst[pos_b]
try_again = False
except IndexError:
pass
def invoke_main() -> None:
execute_benchmarks(ListBenchmarkProvider)
if __name__ == "__main__":
invoke_main()