Skip to content

Commit ba6bc2f

Browse files
committed
np.nonzero: Performance Optimization
1 parent 2484e2a commit ba6bc2f

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/NumSharp.Core/Backends/Default/Indexing/Default.NonZero.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
6262
Debug.Assert(size > 0);
6363
#if _REGEN
6464
#region Compute
65+
Func<int[], int> getOffset = x.Shape.GetOffset;
6566
switch (x.typecode) {
6667
%foreach supported_dtypes, supported_dtypes_lowercase%
6768
case NPTypeCode.#1: {
@@ -71,7 +72,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
7172
int offset;
7273
do
7374
{
74-
offset = x.Shape.GetOffset(coords);
75+
offset = getOffset(coords);
7576
if (!(src[offset] == default(#2)))
7677
nonzeroCoords.Add(coords.CloneArray());
7778
} while (incr.Next() != null);
@@ -83,8 +84,8 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
8384
}
8485
#endregion
8586
#else
86-
8787
#region Compute
88+
Func<int[], int> getOffset = x.Shape.GetOffset;
8889
switch (x.typecode) {
8990
case NPTypeCode.Boolean: {
9091
var incr = new NDCoordinatesIncrementor(x.shape);
@@ -93,8 +94,8 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
9394
int offset;
9495
do
9596
{
96-
offset = x.Shape.GetOffset(coords);
97-
if (src[offset])
97+
offset = getOffset(coords);
98+
if (!(src[offset] == default(bool)))
9899
nonzeroCoords.Add(coords.CloneArray());
99100
} while (incr.Next() != null);
100101

@@ -107,7 +108,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
107108
int offset;
108109
do
109110
{
110-
offset = x.Shape.GetOffset(coords);
111+
offset = getOffset(coords);
111112
if (!(src[offset] == default(byte)))
112113
nonzeroCoords.Add(coords.CloneArray());
113114
} while (incr.Next() != null);
@@ -121,7 +122,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
121122
int offset;
122123
do
123124
{
124-
offset = x.Shape.GetOffset(coords);
125+
offset = getOffset(coords);
125126
if (!(src[offset] == default(short)))
126127
nonzeroCoords.Add(coords.CloneArray());
127128
} while (incr.Next() != null);
@@ -135,7 +136,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
135136
int offset;
136137
do
137138
{
138-
offset = x.Shape.GetOffset(coords);
139+
offset = getOffset(coords);
139140
if (!(src[offset] == default(ushort)))
140141
nonzeroCoords.Add(coords.CloneArray());
141142
} while (incr.Next() != null);
@@ -149,7 +150,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
149150
int offset;
150151
do
151152
{
152-
offset = x.Shape.GetOffset(coords);
153+
offset = getOffset(coords);
153154
if (!(src[offset] == default(int)))
154155
nonzeroCoords.Add(coords.CloneArray());
155156
} while (incr.Next() != null);
@@ -163,7 +164,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
163164
int offset;
164165
do
165166
{
166-
offset = x.Shape.GetOffset(coords);
167+
offset = getOffset(coords);
167168
if (!(src[offset] == default(uint)))
168169
nonzeroCoords.Add(coords.CloneArray());
169170
} while (incr.Next() != null);
@@ -177,7 +178,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
177178
int offset;
178179
do
179180
{
180-
offset = x.Shape.GetOffset(coords);
181+
offset = getOffset(coords);
181182
if (!(src[offset] == default(long)))
182183
nonzeroCoords.Add(coords.CloneArray());
183184
} while (incr.Next() != null);
@@ -191,7 +192,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
191192
int offset;
192193
do
193194
{
194-
offset = x.Shape.GetOffset(coords);
195+
offset = getOffset(coords);
195196
if (!(src[offset] == default(ulong)))
196197
nonzeroCoords.Add(coords.CloneArray());
197198
} while (incr.Next() != null);
@@ -205,7 +206,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
205206
int offset;
206207
do
207208
{
208-
offset = x.Shape.GetOffset(coords);
209+
offset = getOffset(coords);
209210
if (!(src[offset] == default(char)))
210211
nonzeroCoords.Add(coords.CloneArray());
211212
} while (incr.Next() != null);
@@ -219,7 +220,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
219220
int offset;
220221
do
221222
{
222-
offset = x.Shape.GetOffset(coords);
223+
offset = getOffset(coords);
223224
if (!(src[offset] == default(double)))
224225
nonzeroCoords.Add(coords.CloneArray());
225226
} while (incr.Next() != null);
@@ -233,7 +234,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
233234
int offset;
234235
do
235236
{
236-
offset = x.Shape.GetOffset(coords);
237+
offset = getOffset(coords);
237238
if (!(src[offset] == default(float)))
238239
nonzeroCoords.Add(coords.CloneArray());
239240
} while (incr.Next() != null);
@@ -247,7 +248,7 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
247248
int offset;
248249
do
249250
{
250-
offset = x.Shape.GetOffset(coords);
251+
offset = getOffset(coords);
251252
if (!(src[offset] == default(decimal)))
252253
nonzeroCoords.Add(coords.CloneArray());
253254
} while (incr.Next() != null);

0 commit comments

Comments
 (0)