Skip to content

Commit d3d71af

Browse files
committed
enhance: only alloc temp buffer once time (#597)
Signed-off-by: leo <[email protected]>
1 parent 339bcee commit d3d71af

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/Models/NumericSort.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ public static int Compare(string s1, string s2)
1010
int marker1 = 0;
1111
int marker2 = 0;
1212

13+
char[] tmp1 = new char[len1];
14+
char[] tmp2 = new char[len2];
15+
1316
while (marker1 < len1 && marker2 < len2)
1417
{
1518
char c1 = s1[marker1];
1619
char c2 = s2[marker2];
17-
18-
char[] space1 = new char[len1];
19-
char[] space2 = new char[len2];
20-
2120
int loc1 = 0;
2221
int loc2 = 0;
2322

2423
bool isDigit1 = char.IsDigit(c1);
2524
do
2625
{
27-
space1[loc1] = c1;
26+
tmp1[loc1] = c1;
2827
loc1++;
2928
marker1++;
3029

@@ -37,7 +36,7 @@ public static int Compare(string s1, string s2)
3736
bool isDigit2 = char.IsDigit(c2);
3837
do
3938
{
40-
space2[loc2] = c2;
39+
tmp2[loc2] = c2;
4140
loc2++;
4241
marker2++;
4342

@@ -47,9 +46,8 @@ public static int Compare(string s1, string s2)
4746
break;
4847
} while (char.IsDigit(c2) == isDigit2);
4948

50-
string sub1 = new string(space1, 0, loc1);
51-
string sub2 = new string(space2, 0, loc2);
52-
49+
string sub1 = new string(tmp1, 0, loc1);
50+
string sub2 = new string(tmp2, 0, loc2);
5351
int result;
5452
if (isDigit1 && isDigit2)
5553
{

0 commit comments

Comments
 (0)