using System.Text; using BenchmarkHelper; public class BenchmarkDemo { static void Main() { string[] bits = { "a", "b", "c", "d", "e" }; // TestSuite.Create is a generic method on the non-generic TestSuite // class, which returns an instance of the generic // TestSuite class var results = TestSuite.Create("Joining strings", bits, "a b c d e") // Each Add call is returning a new TestSuite // - the original one is left untouched .Add(LoopingWithStringBuilder) .Add(LoopingWithStringConcatenation) // You don't have to specify a method group - but you'll probably // want to give an explicit name if you use .Add(input => string.Join(" ", input), "String.Join") // This returns a ResultSuite (no generics) .RunTests() // Again, scaling returns a new ResultSuite, with all the results // scaled - in this case they'll all have the same number of // iterations .ScaleByBest(ScalingMode.VaryDuration); // There are pairs for name and score, iterations or duration - but // we want name, duration *and* score results.Display(ResultColumns.NameAndDuration | ResultColumns.Score, // Scale the scores to make the best one get 1.0 results.FindBest()); } static string LoopingWithStringBuilder(string[] input) { StringBuilder builder = new StringBuilder(input[0]); for (int i=1; i < input.Length; i++) { builder.Append(" "); builder.Append(input[i]); } return builder.ToString(); } static string LoopingWithStringConcatenation(string[] input) { string ret = input[0]; for (int i=1; i < input.Length; i++) { // At least avoid *one* temporary string per iteration ret = ret + " " + input[i]; } return ret; } }