Created
December 8, 2017 23:23
-
-
Save danmoseley/fb359822de58b081e7aafaf833ab74ca 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() | |
{ | |
sb = new StringBuilder(N * 50); | |
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