Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Last active November 11, 2019 00:15
Show Gist options
  • Save ChrisMcKee/5937656 to your computer and use it in GitHub Desktop.
Save ChrisMcKee/5937656 to your computer and use it in GitHub Desktop.
Pointless string replace benchmark. Just for fun (D commented out because, urgh)
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
public class Program
{
private const string TestData = "Some Random Placé & Some' Chars. ";
private const int Iterations = 1000000;
public static void Main(string[] args)
{
Test(BenchmarkOverhead);
Test(StringCleanA);
Test(StringCleanB);
Test(StringCleanC);
//Test(StringCleanD);
Test(BenchmarkOverhead);
Test(StringCleanA);
Test(StringCleanB);
Test(StringCleanC);
//Test(StringCleanD);
Test(BenchmarkOverhead);
Test(StringCleanA);
Test(StringCleanB);
Test(StringCleanC);
//Test(StringCleanD);
Test(BenchmarkOverhead);
Test(StringCleanA);
Test(StringCleanB);
Test(StringCleanC);
//Test(StringCleanD);
Console.ReadLine();
}
private static void Test(Func<string, bool> function)
{
Console.Write("{0}... ", function.Method.Name);
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < Iterations; i++)
{
function(TestData);
}
sw.Stop();
Console.WriteLine(" {0}ms", sw.ElapsedMilliseconds);
GC.Collect();
}
private static bool BenchmarkOverhead(string str)
{
return true;
}
private static bool StringCleanA(string str)
{
StringExtensionA.Clean(str);
return true;
}
private static bool StringCleanB(string str)
{
StringExtensionB.Clean(str);
return true;
}
private static bool StringCleanC(string str)
{
StringExtensionC.Clean(str);
return true;
}
/*
private static bool StringCleanD(string str)
{
StringExtensionD.Clean(str);
return true;
}
*/
}
public static class StringExtensionA
{
public static string Clean(string s)
{
return (new StringBuilder(s)).Replace("&", "and")
.Replace(",", "")
.Replace(" ", " ")
.Replace(" ", "-")
.Replace("'", "")
.Replace(".", "")
.Replace("eacute;", "é")
.ToString().ToLower();
}
}
public static class StringExtensionB
{
public static string Clean(string s)
{
StringBuilder sb = new StringBuilder(s);
sb.Replace("&", "and");
sb.Replace(",", "");
sb.Replace(" ", " ");
sb.Replace(" ", "-");
sb.Replace("'", "");
sb.Replace(".", "");
sb.Replace("eacute;", "é");
return sb.ToString().ToLower();
}
}
public static class StringExtensionC
{
private static readonly Dictionary<string, string> Replacements = new Dictionary<string, string>()
{
{"&", "and"},
{",", ""},
{" ", " "},
{" ", "-"},
{"'", ""},
{".", ""},
{"eacute;", "é"}
};
/// LINQ Equivalent return Replacements.Keys.Aggregate(s, (current, toReplace) => current.Replace(toReplace, Replacements[toReplace]));
public static string Clean(string s)
{
foreach (string toReplace in Replacements.Keys)
{
s = s.Replace(toReplace, Replacements[toReplace]);
}
return s;
}
}
/*
public static class StringExtensionD
{
public static string Clean(string s)
{
Regex reg = new Regex(@"(\&)|(\,)|(\.)|(\W)|(eacute\;)", RegexOptions.IgnoreCase);
return reg.Replace(s, match =>
{
switch (match.ToString())
{
case "&":
return "and";
case "eacute;":
return "é";
}
return "";
});
}
}
*/
}
BenchmarkOverhead... 13ms
StringCleanA... 2835ms
StringCleanB... 2818ms
StringCleanC... 2123ms
BenchmarkOverhead... 13ms
StringCleanA... 2818ms
StringCleanB... 2811ms
StringCleanC... 2151ms
BenchmarkOverhead... 13ms
StringCleanA... 2840ms
StringCleanB... 2853ms
StringCleanC... 2151ms
BenchmarkOverhead... 13ms
StringCleanA... 2943ms
StringCleanB... 2843ms
StringCleanC... 2199ms
@ChrisMcKee
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment