Skip to content

Commit

Permalink
Use double.Parse on span instead of string in Unix ping (#32423)
Browse files Browse the repository at this point in the history
Avoids a string allocation.
  • Loading branch information
stephentoub authored Feb 17, 2020
1 parent ceaeefe commit 17c6c26
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public static long ParseRoundTripTime(string pingOutput)
int afterTime = timeIndex + "time=".Length;
int msIndex = pingOutput.IndexOf("ms", afterTime, StringComparison.Ordinal);
int numLength = msIndex - afterTime - 1;
string timeSubstring = pingOutput.Substring(afterTime, numLength);
double parsedRtt = double.Parse(timeSubstring, CultureInfo.InvariantCulture);
ReadOnlySpan<char> timeSubstring = pingOutput.AsSpan(afterTime, numLength);
double parsedRtt = double.Parse(timeSubstring, provider: CultureInfo.InvariantCulture);
return (long)Math.Round(parsedRtt);
}
}
Expand Down

0 comments on commit 17c6c26

Please sign in to comment.