Skip to content

Instantly share code, notes, and snippets.

@TheBigNeo
Last active August 28, 2025 06:38
Show Gist options
  • Select an option

  • Save TheBigNeo/6d89cca16e118ef01a5ab56ea4e78113 to your computer and use it in GitHub Desktop.

Select an option

Save TheBigNeo/6d89cca16e118ef01a5ab56ea4e78113 to your computer and use it in GitHub Desktop.
Remove Whitespaces C#
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
|----------------------------- |-----------:|---------:|---------:|------:|--------:|-------:|----------:|------------:|
| StringReplace | 355.6 ns | 1.81 ns | 1.69 ns | 1.00 | 0.01 | 0.0381 | 480 B | 1.00 |
| CharIsWhitespace | 972.5 ns | 18.16 ns | 32.27 ns | 2.73 | 0.09 | 0.2804 | 3528 B | 7.35 |
| CharIsWhitespace_ToCharArray | 1,074.9 ns | 6.66 ns | 5.20 ns | 3.02 | 0.02 | 0.2918 | 3672 B | 7.65 |
| GeneratedRegex | 1,087.5 ns | 2.60 ns | 2.31 ns | 3.06 | 0.02 | 0.0381 | 480 B | 1.00 |
// .Net 9 / C# 13
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Ratio : Mean of the ratio distribution ([Current]/[Baseline])
RatioSD : Standard deviation of the ratio distribution ([Current]/[Baseline])
Gen0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
Alloc Ratio : Allocated memory ratio distribution ([Current]/[Baseline])
1 ns : 1 Nanosecond (0.000000001 sec)
public partial class BenchmarkTest
{
[Benchmark(Baseline = true)]
public string StringReplace()
{
string result = "";
foreach (string value in Values)
{
result = StringReplace(value);
}
return result;
}
[Benchmark]
public string CharIsWhitespace()
{
string result = "";
foreach (string value in Values)
{
result = CharIsWhiteSpace(value);
}
return result;
}
[Benchmark]
public string CharIsWhitespace_ToCharArray()
{
string result = "";
foreach (string value in Values)
{
result = CharIsWhiteSpace_WithToCharArray(value);
}
return result;
}
[GeneratedRegex(@"\s+")]
private static partial Regex MyRegex();
[Benchmark]
public string GeneratedRegex()
{
string result = "";
foreach (string value in Values)
{
result = RegexReplace(value);
}
return result;
}
private static string StringReplace(string input) => input.Replace(" ", "");
private static string CharIsWhiteSpace(string input) => new(input.Where(c => char.IsWhiteSpace(c) is false).ToArray());
private static string CharIsWhiteSpace_WithToCharArray(string input) => new(input.ToCharArray().Where(c => char.IsWhiteSpace(c) is false).ToArray());
private static string RegexReplace(string input) => MyRegex().Replace(input, "");
private static readonly List<string> Values =
[
"1234567890",
"0987654321",
"1112223334",
"5555555555",
// 10-digit numbers (a few spaces)
"12345 67890",
"12 345 67890",
"12 345 678 90",
"123 4567 890",
"1234 567 890",
// 10-digit numbers (many/leading/trailing spaces)
" 1234567890",
"1234567890 ",
" 1234567890 ",
"1 2 3 4 5 6 7 8 9 0",
"12 3456 7890",
// Letter-only usernames
"alice",
"bob",
"charlie",
"david",
"eve",
"frank",
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment