You have to choose between using string.compare or == to compare strings. How would you know which method performs faster?

Today I’ll show you how to use BenchmarkDotNet with .Net Core to answer that question.

Let’s start:

Create a folder for your new project


Open a command prompt an run:

1mkdir benchmark

Create the project


1cd benchmark
2dotnet new console

Add the references to BenchmarkDotNet


1dotnet add package BenchmarkDotNet -v 0.11.0
2dotnet restore

Replace the contents of Program.cs with the following code


 1using System;
 2using System.Linq;
 3using BenchmarkDotNet.Attributes;
 4using BenchmarkDotNet.Running;
 5
 6namespace benchmark
 7{
 8    public class Program
 9    {
10        public static void Main(string[] args)
11        {
12            // Use BenchmarkRunner.Run to Benchmark your code
13            var summary = BenchmarkRunner.Run<StringCompareVsEquals>();
14        }
15    }
16
17    // We are using .Net Core so add we are adding the CoreJobAttribute here.
18    [CoreJob(baseline: true)]
19    [RPlotExporter, RankColumn]
20    public class StringCompareVsEquals
21    {
22        private static Random random = new Random();
23        private string s1;
24        private string s2;
25
26        public static string RandomString(int length)
27        {
28            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
29            return new string(Enumerable.Repeat(chars, length)
30                .Select(s => s[random.Next(s.Length)]).ToArray());
31        }
32
33        // We wil run the the test for 2 diff string lengths: 10 & 100
34        [Params(10, 100)]
35        public int N;
36
37
38        // Create two random strings for each set of params
39        [GlobalSetup]
40        public void Setup()
41        {
42            s1 = RandomString(N);
43            s2 = RandomString(N);
44        }
45
46        // This is the slow way of comparing strings, so let's benchmark it.
47        [Benchmark]
48        public bool Slow() => s1.ToLower() == s2.ToLower();
49
50        // This is the fast way of comparing strings, so let's benchmark it.
51        [Benchmark]
52        public bool Fast() => string.Compare(s1, s2, true) == 0;
53    }
54}

Run the application and interpret the results


Run the following command:

1dotnet run -c release

after a while you should expect an output like the following:

 1// * Summary *
 2
 3BenchmarkDotNet=v0.11.0, OS=Windows 10.0.17134.228 (1803/April2018Update/Redstone4)
 4Intel Core i5-6300U CPU 2.40GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
 5Frequency=2437501 Hz, Resolution=410.2562 ns, Timer=TSC
 6.NET Core SDK=2.1.302
 7  [Host] : .NET Core 2.1.2 (CoreCLR 4.6.26628.05, CoreFX 4.6.26629.01), 64bit RyuJIT
 8  Core   : .NET Core 2.1.2 (CoreCLR 4.6.26628.05, CoreFX 4.6.26629.01), 64bit RyuJIT
 9
10Job=Core  Runtime=Core
11
12 Method |   N |     Mean |     Error |    StdDev |   Median | Scaled | Rank |
13------- |---- |---------:|----------:|----------:|---------:|-------:|-----:|
14   Slow |  10 | 229.6 ns | 11.590 ns | 32.500 ns | 240.8 ns |   1.00 |    1 |
15        |     |          |           |           |          |        |      |
16   Fast |  10 | 195.2 ns | 12.870 ns | 37.948 ns | 205.5 ns |   1.00 |    1 |
17        |     |          |           |           |          |        |      |
18   Slow | 100 | 531.9 ns | 10.320 ns | 14.467 ns | 534.7 ns |   1.00 |    1 |
19        |     |          |           |           |          |        |      |
20   Fast | 100 | 148.2 ns |  3.132 ns |  7.683 ns | 145.1 ns |   1.00 |    1 |

Now you know what method is faster when comparing strings!

Please find all the code used in this post here.

Hope it helps!