|
using System; |
|
using System.Collections.Generic; |
|
using System.Diagnostics; |
|
using System.Linq; |
|
using System.Text.RegularExpressions; |
|
|
|
namespace StringComparisonBenchmark |
|
{ |
|
public static class Program |
|
{ |
|
public static int Main() |
|
{ |
|
RunFirstCharacterTests(10000000); |
|
|
|
Console.WriteLine(); |
|
|
|
RunLastCharacterTests(10000000); |
|
|
|
return 0; |
|
} |
|
|
|
private static void RunFirstCharacterTests(int count) |
|
{ |
|
var startRegex = new Regex(@"^\?"); |
|
var compiledStartRegex = new Regex(@"^\?", RegexOptions.Compiled); |
|
|
|
RunTests("?query", count, |
|
new Dictionary<string, Func<string, bool>> |
|
{ |
|
{ "CharIndexOfEquals", x => x.IndexOf('?').Equals(0) }, |
|
{ "StringIndexOfEquals", x => x.IndexOf("?").Equals(0) }, |
|
{ "StringIndexOfEquals [Ordinal]", x => x.IndexOf("?", StringComparison.Ordinal).Equals(0) }, |
|
{ "StringIndexOfEquals [CurrentCulture]", x => x.IndexOf("?", StringComparison.CurrentCulture).Equals(0) }, |
|
{ "StringIndexOfEquals [OrdinalIgnoreCase]", x => x.IndexOf("?", StringComparison.OrdinalIgnoreCase).Equals(0) }, |
|
{ "IndexerEquals", x => x[0].Equals('?') }, |
|
{ "SubstringEquals", x => x.Substring(0, 1).Equals("?") }, |
|
{ "LinqFirstEquals", x => x.First().Equals('?') }, |
|
{ "StartsWith", x => x.StartsWith("?") }, |
|
{ "StartsWith [Ordinal]", x => x.StartsWith("?", StringComparison.Ordinal) }, |
|
{ "StartsWith [CurrentCulture]", x => x.StartsWith("?", StringComparison.CurrentCulture) }, |
|
{ "StartsWith [OrdinalIgnoreCase]", x => x.StartsWith("?", StringComparison.OrdinalIgnoreCase) }, |
|
{ "RegEx", x => startRegex.IsMatch(x) }, |
|
{ "RegEx [Compiled]", x => compiledStartRegex.IsMatch(x) } |
|
}); |
|
} |
|
|
|
private static void RunLastCharacterTests(int count) |
|
{ |
|
var endRegex = new Regex(@"\?$"); |
|
var compiledEndRegex = new Regex(@"\?$", RegexOptions.Compiled); |
|
|
|
RunTests("query?", count |
|
new Dictionary<string, Func<string, bool>> |
|
{ |
|
{ "CharIndexOfEquals", x => x.IndexOf('?').Equals(x.Length - 1) }, |
|
{ "StringIndexOfEquals", x => x.IndexOf("?").Equals(x.Length - 1) }, |
|
{ "StringIndexOfEquals [Ordinal]", x => x.IndexOf("?", StringComparison.Ordinal).Equals(x.Length - 1) }, |
|
{ "StringIndexOfEquals [CurrentCulture]", x => x.IndexOf("?", StringComparison.CurrentCulture).Equals(x.Length - 1) }, |
|
{ "StringIndexOfEquals [OrdinalIgnoreCase]", x => x.IndexOf("?", StringComparison.OrdinalIgnoreCase).Equals(x.Length - 1) }, |
|
{ "IndexerEquals", x => x[x.Length - 1].Equals('?') }, |
|
{ "SubstringEquals", x => x.Substring(x.Length - 1, 1).Equals("?") }, |
|
{ "LinqLastEquals", x => x.Last().Equals('?') }, |
|
{ "EndsWith", x => x.EndsWith("?") }, |
|
{ "EndsWith [Ordinal]", x => x.EndsWith("?", StringComparison.Ordinal) }, |
|
{ "EndsWith [CurrentCulture]", x => x.EndsWith("?", StringComparison.CurrentCulture) }, |
|
{ "EndsWith [OrdinalIgnoreCase]", x => x.EndsWith("?", StringComparison.OrdinalIgnoreCase) }, |
|
{ "RegEx", x => endRegex.IsMatch(x) }, |
|
{ "RegEx [Compiled]", x => compiledEndRegex.IsMatch(x) } |
|
}); |
|
} |
|
|
|
private static void RunTests(string value, int count, IEnumerable<KeyValuePair<string, Func<string, bool>>> tests) |
|
{ |
|
var results = new Dictionary<string, long>(); |
|
|
|
foreach (var test in tests) |
|
{ |
|
var stopwatch = Stopwatch.StartNew(); |
|
|
|
for (var i = 0; i < count; i++) |
|
{ |
|
if (!test.Value.Invoke(value)) |
|
{ |
|
throw new Exception("Check failed."); |
|
} |
|
} |
|
|
|
stopwatch.Stop(); |
|
|
|
results.Add(test.Key, stopwatch.ElapsedMilliseconds); |
|
} |
|
|
|
foreach (var result in results.OrderBy(x => x.Value)) |
|
{ |
|
Console.WriteLine("{0,-40}: {1}ms", result.Key, result.Value); |
|
} |
|
} |
|
} |
|
} |