Skip to content

Commit 3402b00

Browse files
authored
Merge pull request #168 from static-frame/167/cap-alloc
Improvements to `AK_nonzero_1d`
2 parents 37eb835 + 233b540 commit 3402b00

File tree

2 files changed

+278
-86
lines changed

2 files changed

+278
-86
lines changed

doc/articles/nonzero_1d.py

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ def __call__(self):
4040
_ = np.nonzero(self.array)[0]
4141

4242
class NPNonZeroInt64Convert(ArrayProcessor):
43-
NAME = 'np.nonzero().astype(np.int64)'
43+
NAME = 'np.nonzero()\n.astype(np.int64)'
4444
SORT = 3
4545

4646
def __call__(self):
4747
_ = np.nonzero(self.array)[0].astype(np.int64)
4848

4949

5050
#-------------------------------------------------------------------------------
51-
NUMBER = 100
51+
NUMBER = 200
5252

5353
def seconds_to_display(seconds: float) -> str:
5454
seconds /= NUMBER
@@ -92,13 +92,13 @@ def plot_performance(frame):
9292
title = f'{cat_label:.0e}\n{FixtureFactory.DENSITY_TO_DISPLAY[fixture_label]}'
9393

9494
ax.set_title(title, fontsize=6)
95-
ax.set_box_aspect(0.75) # makes taller tan wide
95+
ax.set_box_aspect(0.75) # makes taller than wide
9696
time_max = fixture['time'].max()
9797
ax.set_yticks([0, time_max * 0.5, time_max])
9898
ax.set_yticklabels(['',
9999
seconds_to_display(time_max * .5),
100100
seconds_to_display(time_max),
101-
], fontsize=6)
101+
], fontsize=4)
102102
# ax.set_xticks(x, names_display, rotation='vertical')
103103
ax.tick_params(
104104
axis='x',
@@ -108,20 +108,20 @@ def plot_performance(frame):
108108
labelbottom=False,
109109
)
110110

111-
fig.set_size_inches(9, 4) # width, height
111+
fig.set_size_inches(10, 4) # width, height
112112
fig.legend(post, names_display, loc='center right', fontsize=6)
113113
# horizontal, vertical
114114
fig.text(.05, .96, f'nonzero_1d() Performance: {NUMBER} Iterations', fontsize=10)
115115
fig.text(.05, .90, get_versions(), fontsize=6)
116116

117117
fp = '/tmp/nonzero.png'
118118
plt.subplots_adjust(
119-
left=0.075,
119+
left=0.05,
120120
bottom=0.05,
121-
right=0.80,
121+
right=0.85,
122122
top=0.85,
123123
wspace=0.9, # width
124-
hspace=0.2,
124+
hspace=0.0,
125125
)
126126
# plt.rcParams.update({'font.size': 22})
127127
plt.savefig(fp, dpi=300)
@@ -138,15 +138,19 @@ class FixtureFactory:
138138
NAME = ''
139139

140140
@staticmethod
141-
def get_array(size: int) -> np.ndarray:
142-
return np.full(size, False, dtype=bool)
141+
def get_array(size: int, contiguous: bool = True) -> np.ndarray:
142+
if contiguous:
143+
return np.full(size, False, dtype=bool)
144+
# take every other value to force non-contigous data
145+
return np.full(size * 2, False, dtype=bool)[::2]
143146

144147
def _get_array_filled(
145148
size: int,
146149
start_third: int, #0, 1 or 2
147150
density: float, # less than 1
151+
contiguous: bool,
148152
) -> np.ndarray:
149-
a = FixtureFactory.get_array(size)
153+
a = FixtureFactory.get_array(size, contiguous)
150154
count = size * density
151155
start = int(len(a) * (start_third/3))
152156
length = len(a) - start
@@ -161,10 +165,14 @@ def get_label_array(cls, size: int) -> tp.Tuple[str, np.ndarray]:
161165
return cls.NAME, array
162166

163167
DENSITY_TO_DISPLAY = {
164-
'single': '1 True',
165-
'quarter': '25% True',
166-
'half': '50% True',
167-
'full': '100% True',
168+
'single-c': '1 True C',
169+
'quarter-c': '25% True C',
170+
'half-c': '50% True C',
171+
'full-c': '100% True C',
172+
'single-nc': '1 True NC',
173+
'quarter-nc': '25% True NC',
174+
'half-nc': '50% True NC',
175+
'full-nc': '100% True NC',
168176
}
169177

170178
# POSITION_TO_DISPLAY = {
@@ -174,35 +182,68 @@ def get_label_array(cls, size: int) -> tp.Tuple[str, np.ndarray]:
174182

175183

176184
class FFSingle(FixtureFactory):
177-
NAME = 'single'
185+
NAME = 'single-c'
186+
187+
@staticmethod
188+
def get_array(size: int) -> np.ndarray:
189+
a = FixtureFactory.get_array(size, contiguous=True)
190+
a[len(a) // 2] = True
191+
return a
192+
193+
class FFSingleNC(FixtureFactory):
194+
NAME = 'single-nc'
178195

179196
@staticmethod
180197
def get_array(size: int) -> np.ndarray:
181-
a = FixtureFactory.get_array(size)
198+
a = FixtureFactory.get_array(size, contiguous=False)
182199
a[len(a) // 2] = True
183200
return a
184201

185202
class FFQuarter(FixtureFactory):
186-
NAME = 'quarter'
203+
NAME = 'quarter-c'
204+
205+
@classmethod
206+
def get_array(cls, size: int) -> np.ndarray:
207+
return cls._get_array_filled(size, start_third=0, density=0.25, contiguous=True)
208+
209+
class FFQuarterNC(FixtureFactory):
210+
NAME = 'quarter-nc'
187211

188212
@classmethod
189213
def get_array(cls, size: int) -> np.ndarray:
190-
return cls._get_array_filled(size, start_third=0, density=0.25)
214+
return cls._get_array_filled(size, start_third=0, density=0.25, contiguous=False)
215+
191216

192217
class FFHalf(FixtureFactory):
193-
NAME = 'half'
218+
NAME = 'half-c'
194219

195220
@classmethod
196221
def get_array(cls, size: int) -> np.ndarray:
197-
return cls._get_array_filled(size, start_third=0, density=0.5)
222+
return cls._get_array_filled(size, start_third=0, density=0.5, contiguous=True)
223+
224+
class FFHalfNC(FixtureFactory):
225+
NAME = 'half-nc'
226+
227+
@classmethod
228+
def get_array(cls, size: int) -> np.ndarray:
229+
return cls._get_array_filled(size, start_third=0, density=0.5, contiguous=False)
230+
198231

199232

200233
class FFFull(FixtureFactory):
201-
NAME = 'full'
234+
NAME = 'full-c'
202235

203236
@classmethod
204237
def get_array(cls, size: int) -> np.ndarray:
205-
return cls._get_array_filled(size, start_third=0, density=1)
238+
return cls._get_array_filled(size, start_third=0, density=1, contiguous=True)
239+
240+
241+
class FFFullNC(FixtureFactory):
242+
NAME = 'full-nc'
243+
244+
@classmethod
245+
def get_array(cls, size: int) -> np.ndarray:
246+
return cls._get_array_filled(size, start_third=0, density=1, contiguous=False)
206247

207248

208249
def get_versions() -> str:
@@ -218,15 +259,20 @@ def get_versions() -> str:
218259

219260
CLS_FF = (
220261
FFSingle,
262+
FFSingleNC,
221263
FFQuarter,
264+
FFQuarterNC,
222265
FFHalf,
266+
FFHalfNC,
223267
FFFull,
268+
FFFullNC,
269+
224270
)
225271

226272

227273
def run_test():
228274
records = []
229-
for size in (100_000, 1_000_000, 10_000_000):
275+
for size in (10_000, 100_000, 1_000_000, 10_000_000):
230276
for ff in CLS_FF:
231277
fixture_label, fixture = ff.get_label_array(size)
232278
for cls in CLS_PROCESSOR:

0 commit comments

Comments
 (0)