Skip to content

Instantly share code, notes, and snippets.

@bbowyersmyth
Created February 21, 2017 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbowyersmyth/9514af463745528d8d290e7cd2492660 to your computer and use it in GitHub Desktop.
Save bbowyersmyth/9514af463745528d8d290e7cd2492660 to your computer and use it in GitHub Desktop.
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