public
Last active

Pointless string replace benchmark. Just for fun

  • Download Gist
pointless_1321343.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
 
internal class Program
{
private const string TestData = "Some Random Place & Some' Chars. ";
private const int Iterations = 1000000;
 
private 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"},
{",", ""},
{" ", " "}
/* etc */
};
 
/// 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 "";
});
}
}
}
results.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
BenchmarkOverhead... 13ms
StringCleanA... 2842ms
StringCleanB... 2879ms
StringCleanC... 1064ms
StringCleanD... 27764ms
BenchmarkOverhead... 12ms
StringCleanA... 2841ms
StringCleanB... 2888ms
StringCleanC... 1074ms
StringCleanD... 28005ms
BenchmarkOverhead... 12ms
StringCleanA... 2859ms
StringCleanB... 2895ms
StringCleanC... 1074ms
StringCleanD... 27981ms

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.