.NET 8 — LINQ Performance Benchmark

Victor Magalhães
5 min readOct 6, 2023

--

LINQ Benchmark

In the ever-evolving world of software development, performance is a critical aspect that can make or break an application. With each new iteration of a programming framework, developers eagerly anticipate enhancements that promise better speed and efficiency. In this article, we delve into a crucial comparison: the performance of LINQ (Language Integrated Query) in .NET 8 versus its predecessor, .NET 7. We’ll explore the notable improvements, benchmarks, and optimizations that have been introduced in .NET 8, shedding light on how these advancements impact the efficiency of LINQ operations. So, fasten your seatbelts as we embark on a journey to discover how .NET 8’s LINQ stacks up against .NET 7 in the realm of performance.

I also recommend reading the official Microsoft article on the subject.

https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/

Setup

Setup

Aggregation and Calculation Methods

Aggregation and Calculation Methods

— Sum: Calculates the sum of values in a numeric sequence.
Count: Returns the number of elements in a sequence.
Min: Finds the minimum value in a sequence.
Max: Finds the maximum value in a sequence.
Average: Calculates the average of values in a numeric sequence.
Aggregate: Performs custom aggregation on a sequence.

Result Analysis

Benchmark Result

For all operations (Sum, Count, Max, Min, Average, and Aggregate), .NET 8 consistently outperforms .NET 7. The performance improvement ranges from approximately 3.47x faster for the Count operation to 4.22x faster for the Aggregate operation. This indicates that .NET 8 brings substantial performance optimizations compared to the previous version.

The Aggregate operation shows the most significant performance improvement, being 4.22x faster in .NET 8 compared to .NET 7. This suggests that the optimizations in .NET 8 have a particularly strong impact on data aggregation operations.

Filtering Methods

Filtering Methods

— Where: Used to filter elements based on a specified condition.
OfType: Used to filter elements of a sequence based on their type.
FirstOrDefault: Return the first element that meets a specified condition or the default value if no element is found.
SingleOrDefault: Return the single element that meets a specified condition or the default value if there are zero or more than one matching element.
LastOrDefault: Return the last element that meets a specified condition or the default value if no element is found.
Take: Allow you to select a specific number of elements.
Skip: Allow you to skip a specific number of elements.
Distinct: Removes duplicate elements from a sequence.

Result Analysis

Benchmark Result

The benchmark assessed the performance of data query operations in both .NET 7.0 and .NET 8.0. The results showcase significant improvements in version 8.0 across several key operations. Operations such as FirstOrDefault, SingleOrDefault, LastOrDefault, Take and Skip exhibited performance gains of up to 3.92 times faster compared to version 7.0. Additionally, the Where operation also demonstrated a 1.27x performance boost in version 8.0. This translates to faster response times, enhanced scalability, and a smoother user experience for applications utilizing these data query operations. Conversely, operations such as OfType and Distinct showed more modest or no improvements in version 8.0.

Verification Methods

Verification Methods

— Any: checks if there is at least one element in the sequence that satisfies a specified condition.
All: verifies if all elements in the sequence meet a specified condition.
Contains: checks if a sequence contains a specific element
SequenceEqual: method verifies if two sequences are equal

Result Analysis

Benchmark Result

The .NET 8 benchmarks reveal significant performance improvements compared to .NET 7.0 in common operations. In .NET 8.0, these operations are substantially faster, with performance improvements ranging from 1.51x to 3.61x compared to .NET 7.0. These optimizations benefit developers by making their applications more responsive and efficient, especially in scenarios involving data manipulation in collections. It’s evident that Microsoft is investing in improving the performance of .NET, which is positive news for the developer community.

Conversion Methods

Conversion Methods

ToList: Converts the collection into a List<int>.
ToArray: Converts the collection into an int[] array.
ToHashSet: Converts the collection into a HashSet<int>.

Result Analysis

Result Analysis

The results demonstrate that in .NET 8.0, these operations are significantly faster compared to .NET 7.0, with performance improvements of approximately 3.5 times for ToList(), 3.22 times for ToArray(), and 2.3 times for ToHashSet.

Conclusion

In summary, based on the provided data, .NET 8 demonstrates considerably better performance compared to .NET 7 in various data processing operations, making it an attractive choice for developers seeking performance enhancements in their applications.

--

--

Victor Magalhães
Victor Magalhães

Written by Victor Magalhães

Software Engineer | C# | .NET | Back-end

No responses yet