Created
August 3, 2017 11:12
-
-
Save davkean/96e7a976ef0357c7c4ee724d77d0e8e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Attributes.Jobs; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Jobs; | |
using Microsoft.VisualStudio.ProjectSystem; | |
using PathHelpers; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.CompilerServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
[LegacyJitX86Job, LegacyJitX64Job, RyuJitX64Job] | |
public class IndexOfAny | |
{ | |
private static readonly char[] s_charsToEscape = { '%', '*', '?', '@', '$', '(', ')', ';', '\'' }; | |
private string[] Names = { "FooBar.cs", "Folder\\Bar\\Foo\\Bar\\FooBar.cs", "$(FooBar)", "**\\*.cs", "*.cs", "Bar.cs;Foo.cs", ";Bar.cs", "\\Bar.cs" }; | |
[Params(0, 1, 2, 3, 4, 5, 6, 7)] | |
public int InputIndex { get; set; } | |
[Benchmark(Description = "Hand written loop over array")] | |
public void IndexOfAny_HandWrittenLoop() | |
{ | |
var name = Names[InputIndex]; | |
for (int i = 0; i < 100000; i++) | |
{ | |
ContainsHelper.IndexOfAny_HandWrittenLoop(name, s_charsToEscape); | |
} | |
} | |
[Benchmark(Baseline = true, Description ="IndexOfAny")] | |
public void IndexOfAny_Baseline() | |
{ | |
var name = Names[InputIndex]; | |
for (int i = 0; i < 100000; i++) | |
{ | |
name.IndexOfAny(s_charsToEscape); | |
} | |
} | |
[Benchmark(Description="Hand written loop over hardcoded chars")] | |
public void IndexOfAny_HandWrittenLoop_HardcodedChars() | |
{ | |
var name = Names[InputIndex]; | |
for (int i = 0; i < 100000; i++) | |
{ | |
ContainsHelper.IndexOfAny_HandWrittenLoop_HardcodedChars(name); | |
} | |
} | |
} | |
internal class ContainsHelper | |
{ | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
public static int IndexOfAny_HandWrittenLoop_HardcodedChars(string value) | |
{ | |
for (int i = 0; i < value.Length; i++) | |
{ | |
switch (value[i]) | |
{ | |
case '%': | |
case '*': | |
case '?': | |
case '@': | |
case '$': | |
case '(': | |
case ')': | |
case ';': | |
case '\'': | |
return i; | |
} | |
} | |
return -1; | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
public static int IndexOfAny_HandWrittenLoop(string value, char[] array) | |
{ | |
if (array.Length == 0) | |
return 0; | |
for (int i = 0; i < value.Length; i++) | |
{ | |
char c = value[i]; | |
foreach (var ch in array) | |
{ | |
if (c == ch) | |
return i; | |
} | |
} | |
return -1; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BenchmarkDotNet.Running.BenchmarkRunner.Run<IndexOfAny>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment