Skip to content

Commit 921083f

Browse files
committed
added arraymap performance
1 parent c16d7d0 commit 921083f

File tree

4 files changed

+1165
-0
lines changed

4 files changed

+1165
-0
lines changed

performance/fixtures.py

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
import typing as tp
2+
3+
4+
import numpy as np
5+
6+
from arraymap import AutoMap
7+
from arraymap import FrozenAutoMap
8+
9+
10+
class PayLoad:
11+
def __init__(self, array: np.ndarray):
12+
self.array = array
13+
self.list = list(array)
14+
self.faml = FrozenAutoMap(self.list)
15+
self.fama = FrozenAutoMap(self.array)
16+
self.ama = AutoMap(self.array)
17+
self.d = dict(zip(self.list, range(len(self.list))))
18+
self.sel_array = array[(np.arange(len(array)) % 2) == 0]
19+
self.sel_scalar = list(self.sel_array)
20+
21+
22+
# -------------------------------------------------------------------------------
23+
INT_START = 500 # avoid cached ints starting at 256
24+
25+
26+
class FixtureFactory:
27+
NAME = ""
28+
SORT = 0
29+
CACHE = {} # can be shared for all classes
30+
31+
@staticmethod
32+
def get_array(size: int) -> np.ndarray:
33+
raise NotImplementedError()
34+
35+
@classmethod
36+
def get_label_array(cls, size: int) -> tp.Tuple[str, PayLoad]:
37+
key = (cls, size)
38+
if key not in cls.CACHE:
39+
pl = PayLoad(cls.get_array(size))
40+
cls.CACHE[key] = pl
41+
return cls.NAME, cls.CACHE[key]
42+
43+
44+
class FFInt64(FixtureFactory):
45+
NAME = "int64"
46+
SORT = 0
47+
48+
@staticmethod
49+
def get_array(size: int) -> np.ndarray:
50+
array = np.arange(INT_START, INT_START + size, dtype=np.int64)
51+
array.flags.writeable = False
52+
return array
53+
54+
55+
class FFInt32(FixtureFactory):
56+
NAME = "int32"
57+
SORT = 1
58+
59+
@staticmethod
60+
def get_array(size: int) -> np.ndarray:
61+
array = np.arange(INT_START, INT_START + size, dtype=np.int32)
62+
array.flags.writeable = False
63+
return array
64+
65+
66+
class FFUInt64(FixtureFactory):
67+
NAME = "uint64"
68+
SORT = 2
69+
70+
@staticmethod
71+
def get_array(size: int) -> np.ndarray:
72+
array = np.arange(INT_START, INT_START + size, dtype=np.uint64)
73+
array.flags.writeable = False
74+
return array
75+
76+
77+
class FFUInt32(FixtureFactory):
78+
NAME = "uint32"
79+
SORT = 3
80+
81+
@staticmethod
82+
def get_array(size: int) -> np.ndarray:
83+
array = np.arange(INT_START, INT_START + size, dtype=np.uint32)
84+
array.flags.writeable = False
85+
return array
86+
87+
88+
class FFFloat64(FixtureFactory):
89+
NAME = "float64"
90+
SORT = 4
91+
92+
@staticmethod
93+
def get_array(size: int) -> np.ndarray:
94+
array = (np.arange(INT_START, INT_START + size) * 0.5).astype(np.float64)
95+
array.flags.writeable = False
96+
return array
97+
98+
99+
class FFFloat32(FixtureFactory):
100+
NAME = "float32"
101+
SORT = 5
102+
103+
@staticmethod
104+
def get_array(size: int) -> np.ndarray:
105+
array = (np.arange(INT_START, INT_START + size) * 0.5).astype(np.float32)
106+
array.flags.writeable = False
107+
return array
108+
109+
110+
def get_string_array(size: int, char_count: int, kind: str) -> str:
111+
fmt = f"-<{char_count}"
112+
array = np.array(
113+
[
114+
f"{hex(e) * (char_count // 8)}".format(fmt)
115+
for e in range(INT_START, INT_START + size)
116+
],
117+
dtype=f"{kind}{char_count}",
118+
)
119+
array.flags.writeable = False
120+
return array
121+
122+
123+
class FFU8(FixtureFactory):
124+
NAME = "U8"
125+
SORT = 6
126+
127+
@staticmethod
128+
def get_array(size: int) -> np.ndarray:
129+
return get_string_array(size, 8, "U")
130+
131+
132+
class FFU16(FixtureFactory):
133+
NAME = "U16"
134+
SORT = 7
135+
136+
@staticmethod
137+
def get_array(size: int) -> np.ndarray:
138+
return get_string_array(size, 16, "U")
139+
140+
141+
class FFU32(FixtureFactory):
142+
NAME = "U32"
143+
SORT = 8
144+
145+
@staticmethod
146+
def get_array(size: int) -> np.ndarray:
147+
return get_string_array(size, 32, "U")
148+
149+
150+
class FFU64(FixtureFactory):
151+
NAME = "U64"
152+
SORT = 9
153+
154+
@staticmethod
155+
def get_array(size: int) -> np.ndarray:
156+
return get_string_array(size, 64, "U")
157+
158+
159+
class FFU128(FixtureFactory):
160+
NAME = "U128"
161+
SORT = 10
162+
163+
@staticmethod
164+
def get_array(size: int) -> np.ndarray:
165+
return get_string_array(size, 128, "U")
166+
167+
168+
class FFS8(FixtureFactory):
169+
NAME = "S8"
170+
SORT = 11
171+
172+
@staticmethod
173+
def get_array(size: int) -> np.ndarray:
174+
return get_string_array(size, 8, "S")
175+
176+
177+
class FFS16(FixtureFactory):
178+
NAME = "S16"
179+
SORT = 12
180+
181+
@staticmethod
182+
def get_array(size: int) -> np.ndarray:
183+
return get_string_array(size, 16, "S")
184+
185+
186+
class FFS32(FixtureFactory):
187+
NAME = "S32"
188+
SORT = 13
189+
190+
@staticmethod
191+
def get_array(size: int) -> np.ndarray:
192+
return get_string_array(size, 32, "S")
193+
194+
195+
class FFS64(FixtureFactory):
196+
NAME = "S64"
197+
SORT = 14
198+
199+
@staticmethod
200+
def get_array(size: int) -> np.ndarray:
201+
return get_string_array(size, 64, "S")
202+
203+
204+
class FFS128(FixtureFactory):
205+
NAME = "S128"
206+
SORT = 15
207+
208+
@staticmethod
209+
def get_array(size: int) -> np.ndarray:
210+
return get_string_array(size, 128, "S")
211+
212+
213+
class FFDTY(FixtureFactory):
214+
NAME = "dt[Y]"
215+
SORT = 20
216+
217+
@staticmethod
218+
def get_array(size: int) -> np.ndarray:
219+
array = np.arange(INT_START, INT_START + size, dtype="datetime64[Y]")
220+
array.flags.writeable = False
221+
return array
222+
223+
224+
class FFDTD(FixtureFactory):
225+
NAME = "dt[D]"
226+
SORT = 21
227+
228+
@staticmethod
229+
def get_array(size: int) -> np.ndarray:
230+
array = np.arange(INT_START, INT_START + size, dtype="datetime64[D]")
231+
array.flags.writeable = False
232+
return array
233+
234+
235+
class FFDTs(FixtureFactory):
236+
NAME = "dt[s]"
237+
SORT = 22
238+
239+
@staticmethod
240+
def get_array(size: int) -> np.ndarray:
241+
array = np.arange(INT_START, INT_START + size, dtype="datetime64[s]")
242+
array.flags.writeable = False
243+
return array
244+
245+
246+
class FFDTns(FixtureFactory):
247+
NAME = "dt[ns]"
248+
SORT = 23
249+
250+
@staticmethod
251+
def get_array(size: int) -> np.ndarray:
252+
array = np.arange(INT_START, INT_START + size, dtype="datetime64[ns]")
253+
array.flags.writeable = False
254+
return array
255+
256+
257+
class FFObject(FixtureFactory):
258+
NAME = "object"
259+
SORT = 5
260+
261+
@staticmethod
262+
def get_array(size: int) -> np.ndarray:
263+
ints = np.arange(INT_START, INT_START + size)
264+
array = ints.astype(object)
265+
266+
target = 1 == ints % 3
267+
array[target] = ints[target] * 0.5
268+
269+
target = 2 == ints % 3
270+
array[target] = np.array([hex(e) for e in ints[target]])
271+
272+
array.flags.writeable = False
273+
return array

0 commit comments

Comments
 (0)