Created
December 8, 2017 21:40
-
-
Save danmoseley/873f5bd1086d3af1cf5471edca564b91 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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System; | |
using System.Text; | |
namespace ConsoleApplication1 | |
{ | |
public class Program | |
{ | |
private StringBuilder sb = new StringBuilder(); | |
private string expected; | |
[Params(100, 1000, 5000)] | |
public int N; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
for (int i = 0; i < N; i++) | |
{ | |
sb.Append(new string('x', 50)); | |
} | |
expected = sb.ToString(); | |
} | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<Program>(); | |
} | |
[Benchmark] | |
public bool ToStringFirst() | |
{ | |
// Console.WriteLine(Object.ReferenceEquals(a, b)); // returns false | |
return (expected == sb.ToString()); | |
} | |
[Benchmark] | |
public bool IndexedBackward() | |
{ | |
if (expected.Length != sb.Length) return false; | |
for (int i = expected.Length - 1; i >= 0; i--) | |
{ | |
if (expected[i] != sb[i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
[Benchmark] | |
public bool IndexedForward() | |
{ | |
if (expected.Length != sb.Length) return false; | |
for (int i = 0; i < expected.Length ; i++) | |
{ | |
if (expected[i] != sb[i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment