.NET 6, .NET 7 and .NET 8 Performance Comparison

As per Microsoft .NET 8 is more optimized than .NET 7 and .NET 6. Also, .NET 8 is the fastest version compared to .NET 6 and 7.

In this article, we will compare .NET 6, 7 and 8 performance with the BenchmarkDotNet library.

Read more about the use of BenchmarkDotNet library.

Benchmark Compare of .NET 6, .NET 7 and .NET 8

So, to demonstrate the performance, we are going to use a small code snippet:

    public void Method1()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        IEnumerable<int> numberEnumerable = numbers;
        var result = numberEnumerable.Where(n => n % 2 == 0);
        foreach (int num in result)
        {
            Console.WriteLine(num);
        }
    }

Here, we are using only one method. The method just finds the even number using IEnumerable.

You may explore IQueryable and IEnumerebale

To analyse the benchmark, firstly we will install the BenchmarkDotNet NuGet package.

Now, create a console application and select the framework as .NET 6.

The final code snippet will look like this:

BenchmarkRunner.Run<BenchmarkLogicLookup>();

[MemoryDiagnoser]
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class BenchmarkLogicLookup
{
    [Benchmark]
    public void Method1()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        IEnumerable<int> numberEnumerable = numbers;
        var result = numberEnumerable.Where(n => n % 2 == 0);
        foreach (int num in result)
        {
            Console.WriteLine(num);
        }
    }
}

Let’s run the project and see the output of .NET 6:

// * Summary *

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3447/23H2/2023Update/SunValley3)
AMD Ryzen 5 5500U with Radeon Graphics, 1 CPU, 12 logical and 6 physical cores

  [Host]     : .NET 6.0.26 (6.0.2623.60508), X64 RyuJIT AVX2 [AttachedDebugger]
  DefaultJob : .NET 6.0.26 (6.0.2623.60508), X64 RyuJIT AVX2


| Method  | Mean     | Error    | StdDev   | Median   | Rank | Allocated |
|-------- |---------:|---------:|---------:|---------:|-----:|----------:|
| Method1 | 627.0 us | 28.80 us | 81.71 us | 596.5 us |    1 |     150 B |

Next, create another console application and this time target framework should be .NET 7.

We will copy the same code snippet and paste it in Program.cs file.

So, let’s run the program and note down the output of .NET 7:

// * Summary *

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3447/23H2/2023Update/SunValley3)
AMD Ryzen 5 5500U with Radeon Graphics, 1 CPU, 12 logical and 6 physical cores

  [Host]     : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2 [AttachedDebugger]
  DefaultJob : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2


| Method  | Mean     | Error    | StdDev   | Rank | Allocated |
|-------- |---------:|---------:|---------:|-----:|----------:|
| Method1 | 596.7 us | 11.36 us | 13.08 us |    1 |     144 B |

Similarly, create another console application and the target framework is .NET 8.

Use the same code snippet.

Let’s run the project to note down the output of .NET 8:

// * Summary *

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3447/23H2/2023Update/SunValley3)
AMD Ryzen 5 5500U with Radeon Graphics, 1 CPU, 12 logical and 6 physical cores

  [Host]     : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2 [AttachedDebugger]
  DefaultJob : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2


| Method  | Mean     | Error    | StdDev   | Rank | Allocated |
|-------- |---------:|---------:|---------:|-----:|----------:|
| Method1 | 592.8 us | 11.19 us | 12.89 us |    1 |     112 B |

Let’s Compare the Performance

Once we are ready with the output of all 3 projects, let’s compare the performance.

.NET 6.NET 7.NET 8
Average Meantime : 627 usAverage Meantime: 596.7 usAverage Meantime: 592.8 us
Allocated Memory: 150 BAllocated Memory: 144 BAllocated Memory: 112 B
So, based on the output, we observe that the execution time is higher in .NET 6 and lesser in .NET 8 and the same for allocated memory.

These are small differences, however, our code snippet is also simple and does not have any complex logic.

Conclusion

In this article, we compared the performance of the .NET 6, 7 and 8 versions. We used the BenchmarkDotNet library to demonstrate the comparison.

Based on the output, we can say that .NET 8 is the fastest among .NET 6, 7, and 8.

Keep Following: SharePointCafe.NET

Leave a Comment

RSS
YouTube
YouTube
Instagram