Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Pointless string replace benchmark. Just for fun (D commented out because, urgh)

View pointless_1321343.cs
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 154 155 156 157 158 159 160 161 162 163
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 "";
});
}
}
*/
}
View pointless_1321343.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.