From d3d71af0e80d6a7b7836c7cf66fff75219277414 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 24 Oct 2024 17:07:30 +0800 Subject: [PATCH] enhance: only alloc temp buffer once time (#597) Signed-off-by: leo --- src/Models/NumericSort.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Models/NumericSort.cs b/src/Models/NumericSort.cs index 052f538b..bdff95e6 100644 --- a/src/Models/NumericSort.cs +++ b/src/Models/NumericSort.cs @@ -10,21 +10,20 @@ int marker1 = 0; int marker2 = 0; + char[] tmp1 = new char[len1]; + char[] tmp2 = new char[len2]; + while (marker1 < len1 && marker2 < len2) { char c1 = s1[marker1]; char c2 = s2[marker2]; - - char[] space1 = new char[len1]; - char[] space2 = new char[len2]; - int loc1 = 0; int loc2 = 0; bool isDigit1 = char.IsDigit(c1); do { - space1[loc1] = c1; + tmp1[loc1] = c1; loc1++; marker1++; @@ -37,7 +36,7 @@ bool isDigit2 = char.IsDigit(c2); do { - space2[loc2] = c2; + tmp2[loc2] = c2; loc2++; marker2++; @@ -47,9 +46,8 @@ break; } while (char.IsDigit(c2) == isDigit2); - string sub1 = new string(space1, 0, loc1); - string sub2 = new string(space2, 0, loc2); - + string sub1 = new string(tmp1, 0, loc1); + string sub2 = new string(tmp2, 0, loc2); int result; if (isDigit1 && isDigit2) {