Created
February 21, 2017 06:35
-
-
Save bbowyersmyth/9514af463745528d8d290e7cd2492660 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 System; | |
namespace ConsoleApplication2 | |
{ | |
[Config("jobs=RyuJitX64")] | |
public unsafe class LoopWithExit | |
{ | |
public int length = 100; | |
private string test1; | |
private string test2; | |
[Setup] | |
public void Setup() | |
{ | |
test1 = new string('A', length); | |
test2 = new string('A', length); | |
} | |
[Benchmark] | |
public bool LoopReturn() | |
{ | |
return LoopReturn(test1, test2); | |
} | |
[Benchmark] | |
public bool LoopGoto() | |
{ | |
return LoopGoto(test1, test2); | |
} | |
public bool LoopReturn(String strA, String strB) | |
{ | |
int length = strA.Length; | |
fixed (char* ap = strA) fixed (char* bp = strB) | |
{ | |
char* a = ap; | |
char* b = bp; | |
while (length != 0) | |
{ | |
int charA = *a; | |
int charB = *b; | |
if (charA != charB) | |
return false; | |
a++; | |
b++; | |
length--; | |
} | |
return true; | |
} | |
} | |
public static bool LoopGoto(String strA, String strB) | |
{ | |
int length = strA.Length; | |
fixed (char* ap = strA) fixed (char* bp = strB) | |
{ | |
char* a = ap; | |
char* b = bp; | |
while (length != 0) | |
{ | |
int charA = *a; | |
int charB = *b; | |
if (charA != charB) | |
goto ReturnFalse; | |
a++; | |
b++; | |
length--; | |
} | |
return true; | |
ReturnFalse: | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment