Created
September 27, 2015 03:12
-
-
Save bbowyersmyth/099c572ab8fdd5fc48cb to your computer and use it in GitHub Desktop.
This file contains 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 System.Diagnostics; | |
using Xunit.Abstractions; | |
[assembly: CollectionBehavior(DisableTestParallelization = true)] | |
// Remove "static" from "public static unsafe class StringTests" | |
[Fact] | |
public void TestStartsWithPerfMatch1Char() | |
{ | |
string test2 = "abc"; | |
bool result; | |
Profile("StartsWith Perf Match Char", 10000000, () => | |
{ | |
result = test2.StartsWith("a", StringComparison.Ordinal); | |
}); | |
} | |
[Fact] | |
public void TestStartsWithPerfMatch2Char() | |
{ | |
string test1 = "abc"; | |
bool result; | |
Profile("StartsWith Perf Match String", 10000000, () => | |
{ | |
result = test1.StartsWith("ab", StringComparison.Ordinal); | |
}); | |
} | |
[Fact] | |
public void TestStartsWithPerfNoMatchFirstChar() | |
{ | |
string test1 = "abc"; | |
bool result; | |
Profile("StartsWith Perf No-Match first char", 10000000, () => | |
{ | |
result = test1.StartsWith("X", StringComparison.Ordinal); | |
}); | |
} | |
[Fact] | |
public void TestStartsWithPerfNoMatch() | |
{ | |
string test1 = "abc"; | |
bool result; | |
Profile("StartsWith Perf No-Match", 10000000, () => | |
{ | |
result = test1.StartsWith("aX", StringComparison.Ordinal); | |
}); | |
} | |
private readonly ITestOutputHelper output; | |
public StringTests(ITestOutputHelper output) | |
{ | |
this.output = output; | |
} | |
void Profile(string description, int iterations, Action func) | |
{ | |
// warm up | |
func(); | |
// clean up | |
GC.Collect(); | |
var watch = new Stopwatch(); | |
watch.Start(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
func(); | |
} | |
watch.Stop(); | |
output.WriteLine(description); | |
output.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment