Skip to content

Instantly share code, notes, and snippets.

@databyss
Last active February 10, 2017 17:15
Show Gist options
  • Save databyss/b31dc806abf8509a436dfd08f83076a8 to your computer and use it in GitHub Desktop.
Save databyss/b31dc806abf8509a436dfd08f83076a8 to your computer and use it in GitHub Desktop.
Starting character check benchmark
using System;
using System.Diagnostics;
using System.Linq;
namespace BenchmarkTest
{
class Program
{
private static Random random = new Random();
static void Main(string[] args)
{
Console.WriteLine("Starting benchmark tests");
int testCount = 1000000;
Console.WriteLine($"{Environment.NewLine}Starting test: [SINGLE CHAR] [STARTSWITH CHECK] [RANDOM STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"# {RandomString(50)}";
if (testString.StartsWith("#"))
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine($"{Environment.NewLine}Starting test: [DOUBLE CHAR] [STARTSWITH CHECK] [RANDOM STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"// {RandomString(50)}";
if (testString.StartsWith("//"))
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine($"{Environment.NewLine}Starting test: [SINGLE CHAR] [STARTSWITH CHECK] [SAME STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"# This is a sufficiently long string to be a comment. It's very much like a comment. So comment.";
if (testString.StartsWith("#"))
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine($"{Environment.NewLine}Starting test: [DOUBLE CHAR] [STARTSWITH CHECK] [SAME STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"// This is a sufficiently long string to be a comment. It's very much like a comment. So comment.";
if (testString.StartsWith("//"))
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine($"{Environment.NewLine}Starting test: [SINGLE CHAR] [CHAR CHECK] [RANDOM STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"# {RandomString(50)}";
if (testString[0] == '#')
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine($"{Environment.NewLine}Starting test: [DOUBLE CHAR] [CHAR CHECK] [RANDOM STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"// {RandomString(50)}";
if (testString[0] == '/' && testString[1] == '/')
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine($"{Environment.NewLine}Starting test: [SINGLE CHAR] [CHAR CHECK] [SAME STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"# This is a sufficiently long string to be a comment. It's very much like a comment. So comment.";
if (testString[0] == '#')
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine($"{Environment.NewLine}Starting test: [DOUBLE CHAR] [CHAR CHECK] [SAME STRINGS] [COUNT: {testCount:N0}]");
Benchmark(() =>
{
var testString = $"// This is a sufficiently long string to be a comment. It's very much like a comment. So comment.";
if (testString[0] == '/' && testString[1] == '/')
{
// confirmed
}
else
{
// rejected
}
}, testCount);
Console.WriteLine("Complete. Press ENTER to exit.");
// Wait for enter key to be pressed
Console.ReadLine();
}
/// <summary>
/// Generate a random string of the specified length
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
/// <summary>
/// Run a benchmark check against the specified action.
/// </summary>
/// <param name="act"></param>
/// <param name="iterations"></param>
private static void Benchmark(Action act, int iterations)
{
GC.Collect();
act.Invoke(); // run once outside of loop to avoid initialization costs
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
act.Invoke();
}
sw.Stop();
double averageTime = ((double)sw.ElapsedMilliseconds / iterations);
Console.WriteLine($"[TOTAL TIME {sw.ElapsedMilliseconds:N0}ms] [AVERAGE TIME PER {averageTime:N10}ms]");
}
}
}
@databyss
Copy link
Author

databyss commented Feb 10, 2017

Starting benchmark tests

Starting test: [SINGLE CHAR] [STARTSWITH CHECK] [RANDOM STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 2,188ms] [AVERAGE TIME PER 0.0021880000ms]

Starting test: [DOUBLE CHAR] [STARTSWITH CHECK] [RANDOM STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 2,192ms] [AVERAGE TIME PER 0.0021920000ms]

Starting test: [SINGLE CHAR] [STARTSWITH CHECK] [SAME STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 107ms] [AVERAGE TIME PER 0.0001070000ms]

Starting test: [DOUBLE CHAR] [STARTSWITH CHECK] [SAME STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 109ms] [AVERAGE TIME PER 0.0001090000ms]

Starting test: [SINGLE CHAR] [CHAR CHECK] [RANDOM STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 2,026ms] [AVERAGE TIME PER 0.0020260000ms]

Starting test: [DOUBLE CHAR] [CHAR CHECK] [RANDOM STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 2,026ms] [AVERAGE TIME PER 0.0020260000ms]

Starting test: [SINGLE CHAR] [CHAR CHECK] [SAME STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 5ms] [AVERAGE TIME PER 0.0000050000ms]

Starting test: [DOUBLE CHAR] [CHAR CHECK] [SAME STRINGS] [COUNT: 1,000,000]
[TOTAL TIME 8ms] [AVERAGE TIME PER 0.0000080000ms]
Complete. Press ENTER to exit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment