public static void Main() { long seed = ConfigProgramForBenchmarking(); string seedAsString = seed.ToString(); Stopwatch sw = new Stopwatch(); Func <string, string> f; List <string> inputs = new List <string>(); List <string> outputs = new List <string>(); inputs.Add("This is a Warm ONLY up function for\tbest \r\n benchmark results." + seed); outputs.Add("This is a Warm ONLY up function for\tbest \r\n benchmark results." + seed); inputs.Add("Hello World, how are you doing?" + seed); outputs.Add("Hello World, how are you doing?" + seed); inputs.Add("It\twas\t \tso nice to\t\t see you \tin 1950. \t" + seed); outputs.Add("It was so nice to see you in 1950. " + seed); inputs.Add("That car\r\nis sooooooooo fast." + seed); outputs.Add("That car is sooooooooo fast." + seed); inputs.Add(" " + seed); outputs.Add(" " + seed); inputs.Add(" " + seed); outputs.Add(" " + seed); //warm-up timer function f = (x) => seedAsString; long baseVal = TestMethod(f, sw, inputs, outputs, 0, "BASELINE").ElapsedTicks; Console.Clear(); Console.WriteLine(@"| | Time | TEST 1 | TEST 2 | TEST 3 | TEST 4 | TEST 5 |"); Console.WriteLine(@"| Function Name |(ticks)| dup. spaces | spaces+tabs | spaces+CR/LF| "" "" -> "" "" | "" "" -> "" "" |"); Console.WriteLine(@"|---------------------------|-------|-------------|-------------|-------------|-------------|-------------|"); // InPlace Replace by Felipe Machado but modified by Ryan for multi-space removal (http://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin) TestMethod(SwitchStmtBuildSpaceOnly, sw, inputs, outputs, baseVal); // InPlace Replace by Felipe Machado but modified by Ryan for multi-space removal (http://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin) TestMethod(InPlaceCharArraySpaceOnly, sw, inputs, outputs, baseVal); // InPlace Replace by Felipe Machado but modified by Ryan for multi-space removal (http://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin) TestMethod(SwitchStmtBuild, sw, inputs, outputs, baseVal); // InPlace Replace by Felipe Machado but modified by Ryan for multi-space removal (http://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin) TestMethod(SwitchStmtBuild2, sw, inputs, outputs, baseVal); //StringBuilder by David S 2013 (https://stackoverflow.com/a/16035044/2352507) TestMethod(SingleSpacedTrim, sw, inputs, outputs, baseVal); //StringBuilder by fubo (https://stackoverflow.com/a/27502353/2352507 TestMethod(Fubo, sw, inputs, outputs, baseVal); //Split And Join by Jon Skeet (https://stackoverflow.com/a/1280227/2352507) TestMethod(SplitAndJoinOnSpace, sw, inputs, outputs, baseVal); //Regex with compile by Jon Skeet (https://stackoverflow.com/a/1280227/2352507) f = (x) => MultipleSpaces.Replace(x, " "); TestMethod(f, sw, inputs, outputs, baseVal, "RegExWithCompile"); //StringBuilder by user214147 (https://stackoverflow.com/a/2156660/2352507 TestMethod(User214147, sw, inputs, outputs, baseVal); //Regex by Brandon (https://stackoverflow.com/a/1279878/2352507 f = (x) => Regex.Replace(x, @"\s{2,}", " "); TestMethod(f, sw, inputs, outputs, baseVal, "RegExBrandon"); //Regex with non-compile Tim Hoolihan (https://stackoverflow.com/a/1279874/2352507) f = (x) => Regex.Replace(x, @"\s+", " "); TestMethod(f, sw, inputs, outputs, baseVal, "RegExNoCompile"); }
public static void Main(string[] args) { long seed = ConfigProgramForBenchmarking(); Stopwatch sw = new Stopwatch(); string warmup = "This is a Warm up function for best benchmark results." + seed; string input1 = "Hello World, how are you doing?" + seed; string input2 = "It\twas\t \tso nice to\t\t see you \tin 1950. \t" + seed; string correctOutput1 = "Hello World, how are you doing?" + seed; string correctOutput2 = "It\twas\tso nice to\tsee you in 1950. " + seed; string output1,output2; //warm-up timer function sw.Restart(); sw.Stop(); sw.Restart(); sw.Stop(); long baseVal = sw.ElapsedTicks; // InPlace Replace by Felipe Machado but modified by Ryan for multi-space removal (http://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin) output1 = InPlaceCharArraySpaceOnly (warmup); sw.Restart(); output1 = InPlaceCharArraySpaceOnly (input1); output2 = InPlaceCharArraySpaceOnly (input2); sw.Stop(); Console.WriteLine("InPlaceCharArraySpaceOnly : " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); // InPlace Replace by Felipe R. Machado and slightly modified by Ryan for multi-space removal (http://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin) output1 = InPlaceCharArray(warmup); sw.Restart(); output1 = InPlaceCharArray(input1); output2 = InPlaceCharArray(input2); sw.Stop(); Console.WriteLine("InPlaceCharArray: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); //Regex with non-compile Tim Hoolihan (http://stackoverflow.com/a/1279874/2352507) string cleanedString = output1 = Regex.Replace(warmup, @"\s+", " "); sw.Restart(); output1 = Regex.Replace(input1, @"\s+", " "); output2 = Regex.Replace(input2, @"\s+", " "); sw.Stop(); Console.WriteLine("Regex by Tim Hoolihan: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); //Regex with compile by Jon Skeet (http://stackoverflow.com/a/1280227/2352507) output1 = MultipleSpaces.Replace(warmup, " "); sw.Restart(); output1 = MultipleSpaces.Replace(input1, " "); output2 = MultipleSpaces.Replace(input2, " "); sw.Stop(); Console.WriteLine("Regex with compile by Jon Skeet: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); //Split And Join by Jon Skeet (http://stackoverflow.com/a/1280227/2352507) output1 = SplitAndJoinOnSpace(warmup); sw.Restart(); output1 = SplitAndJoinOnSpace(input1); output2 = SplitAndJoinOnSpace(input2); sw.Stop(); Console.WriteLine("Split And Join by Jon Skeet: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); //Regex by Brandon (http://stackoverflow.com/a/1279878/2352507 output1 = Regex.Replace(warmup, @"\s{2,}", " "); sw.Restart(); output1 = Regex.Replace(input1, @"\s{2,}", " "); output2 = Regex.Replace(input2, @"\s{2,}", " "); sw.Stop(); Console.WriteLine("Regex by Brandon: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); //StringBuilder by user214147 (http://stackoverflow.com/a/2156660/2352507 output1 = user214147(warmup); sw.Restart(); output1 = user214147(input1); output2 = user214147(input2); sw.Stop(); Console.WriteLine("StringBuilder by user214147: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); //StringBuilder by fubo (http://stackoverflow.com/a/27502353/2352507 output1 = fubo(warmup); sw.Restart(); output1 = fubo(input1); output2 = fubo(input2); sw.Stop(); Console.WriteLine("StringBuilder by fubo: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); //StringBuilder by David S 2013 (http://stackoverflow.com/a/16035044/2352507) output1 = SingleSpacedTrim(warmup); sw.Restart(); output1 = SingleSpacedTrim(input1); output2 = SingleSpacedTrim(input2); sw.Stop(); Console.WriteLine("StringBuilder(SingleSpacedTrim) by David S: " + (sw.ElapsedTicks - baseVal)); Console.WriteLine(" Trial1:(spaces only) " + (output1 == correctOutput1 ? "PASS " : "FAIL ")); Console.WriteLine(" Trial2:(spaces+tabs) " + (output2 == correctOutput2 ? "PASS " : "FAIL ")); }