BenchmarkDotNet usage examples. Compare String Equals methods, and For vs ForEach.

// ------------------------------------------------------------------------
// String Equals Benchmarks
//
// Source: https://github.com/cmendible/dotnetcore.samples/blob/master/benchmarkdotnet/Program.cs
// Related: https://jonlabelle.com/snippets/view/markdown/powerful-easy-net-library-for-benchmarking
// ------------------------------------------------------------------------

using System;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace benchmark
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Use BenchmarkRunner.Run to Benchmark your code...
            var summary = BenchmarkRunner.Run<StringCompareVsEquals>();
        }
    }

    // We're using .NET Core, so add the CoreJobAttribute here
    [SimpleJob(baseline: true)]
    [RPlotExporter, RankColumn]
    public class StringCompareVsEquals
    {
        private static Random random = new Random();
        private string s1;
        private string s2;

        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());
        }

        // Run the test for 3 diff string lengths... 10, 100 and 10000
        [Params(10, 100, 10000)]
        public int N;


        // Create two random strings for each set of params
        [GlobalSetup]
        public void Setup()
        {
            s1 = RandomString(N);
            s2 = RandomString(N);
        }

        // This is the slow way of comparing strings, so let's benchmark it.
        [Benchmark]
        public bool Slow() => s1.ToLower() == s2.ToLower();

        // This is the fast way of comparing strings, so let's benchmark it.
        [Benchmark]
        public bool Fast() => string.Compare(s1, s2, true) == 0;

        // This is the fastest!!! way of comparing strings, so let's benchmark it.
        [Benchmark]
        public bool Faster() => string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase);
    }
}

// ------------------------------------------------------------------------
// For vs ForEach Benchmarks
//
// Source: https://github.com/cmendible/dotnetcore.samples/blob/master/benchmarkdotnet.for/Program.cs
// Related: https://jonlabelle.com/snippets/view/markdown/powerful-easy-net-library-for-benchmarking
// ------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace benchmark
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Use BenchmarkRunner.Run to Benchmark your code
            var summary = BenchmarkRunner.Run<ForEachVsFor>();
        }
    }

    // We are using .Net Core we are adding the CoreJobAttribute here.
    [SimpleJob(baseline: true)]
    [RPlotExporter, RankColumn]
    public class ForEachVsFor
    {
        private static Random random = new Random();
        private List<int> list;

        public static List<int> RandomIntList(int length)
        {
            int Min = 1;
            int Max = 10;
            return Enumerable
                .Repeat(0, length)
                .Select(i => random.Next(Min, Max))
                .ToList();
        }

        // We wil run the test for 3 diff list sizes
        [Params(10, 100, 1000)]
        public int N;

        [GlobalSetup]
        public void Setup()
        {
            list = RandomIntList(N);
        }

        // Foreach is ~2 times slower than for
        [Benchmark]
        public void Foreach()
        {
            int total = 0;
            foreach (int i in list)
            {
                total += i;
            }
        }

        // For is ~2 times faster than foreach
        [Benchmark]
        public void For()
        {
            int total = 0;
            for (int i = 0; i < list.Count; i++)
            {
                total += list[i];
            }
        }
    }
}