Is Using LINQ in C# Bad for Performance?

Is Using LINQ in C# Bad for Performance?

LINQ (Language INtegrated Query) is a set of features in C# for concise and declarative code. I use it pretty much wherever I can, but sometimes I wonder and people ask: is this a performance trap? It would be painful to have to replace many usages of LINQ with other constructs in addition to time spent finding that to be an actual bottleneck. Let’s do a basic benchmark to see what using LINQ might cost us compared to more traditional constructs such as for and foreach loops, and see if this worry is valid.

LINQ (Language INtegrated Query) is a set of features in C# for concise and declarative code. I use it pretty much wherever I can, but sometimes I wonder and people ask: is this a performance trap? It would be painful to have to replace many usages of LINQ with other constructs in addition to time spent finding that to be an actual bottleneck. Let’s do a basic benchmark to see what using LINQ might cost us compared to more traditional constructs such as for and foreach loops, and see if this worry is valid.

Methodology

I used the BenchmarkDotNet library. The experiment code can be found here. All benchmarks are of functions that operate on Customer arrays, where a Customer is an immutable struct with an int Age and a string Name. Note that these methods would normally be parameterized, taking the Customer array as an argument; however it is easier to use BenchmarkDotNet with methods that take no arguments.

I ran these benchmarks on my laptop, which is a 2020 Dell Inspiron 5591 2-in-1 with Intel Core i5–10210U, 16GB RAM (8GB original, 8GB DDR4–3200 from Crucial), running Windows 10 Home 1909. The code runs on .NET Core 3.1. BenchmarkDotNet runs each experiment — in this case a unique size and implementation — 100 times and measures statistics on those runs. All line charts comparing implementations are of the average run time across those runs.

Benchmark: Iteration

Each implementation returns the number of Customer’s in the array that have an age under 18 (that is, are minors). Baseline implementations using for/foreach:

Image for post

…and two using LINQ Count:

Image for post

Image for post

Image for post

Image for post

Image for post

The LINQ implementation using Select and then Count with no arguments has a similar slope plus a small overhead penalty compared to for/foreach (overlaid on the graph because they’re so close). However the LINQ implementation using Count that takes a predicate function on the elements has a significant performance penalty and the slope of the graph is much steeper. This was a surprising result to me.

performance functional-programming linq c-sharp-programming programming-c c-sharp

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

C programming for Beginners | Learn C Programming | C Language

C Language is an evergreen language and is used widely across different industries, This C programming is a must for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. Great Learning brings you this live session on "Introduction to C". In this live session, we will be covering major concepts in C Programming such as Different Variables, Different Data Types that are being used, its Operators, Flow control statements, Structure, and lot more.

Learn how to perform CRUD operations using C Sharp

QuickStart: CRUD Operations in C Sharp. Learn how to perform CRUD operations using C Sharp for MongoDB databases.

5 Best C# (C-Sharp) Programming Courses for Beginners in 2020

In this article, I am going to share some of the best courses to learn C# or C-Sharp in 2020. These courses are suitable for both beginners and experienced programmers who want to fill gaps in their C# knowledge.

C++ Functions Example | What is Function in C++

C++ Function is the block of organized, reusable code that can be used to perform the single, related action. The function performs a specific task.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.