Skip to content

Instantly share code, notes, and snippets.

@bbowyersmyth
Created September 27, 2015 03:12
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/099c572ab8fdd5fc48cb to your computer and use it in GitHub Desktop.
Save bbowyersmyth/099c572ab8fdd5fc48cb to your computer and use it in GitHub Desktop.
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