473,795 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LINQ Overhead

Consider the following:

List<intints = new List<int>();
for (int i = 0; i < 20; i++) {
ints.Add(i);
}

// EXAMPLE 1
var strings =
from i in ints
where i 0 && i < 10 select i.ToString();

foreach (string s in strings) {
Console.WriteLi ne(s);
}

// EXAMPLE 2
foreach (int i in ints) {
if (i 0 && i < 10) {
Console.WriteLi ne(i.ToString() );
}
}
Now, what kind of overhead are we looking at for using LINQ in EXAMPLE 1
compared to using just the 'if' statement as shown in EXAMPLE 2? I have
multiple code blocks that I've redone to use LINQ and have started thinking
if I shouldn't have when a simple 'if' statement would do the trick just
fine....what do you peeps think?

Thanks a bunch!

Mythran

::I'm NOT a zero, I'm a one (just don't ask my wife)::

Oct 24 '08 #1
5 2599
Best way to know is to use bigger numbers and try it. You will be surprised
at LINQ's performance.

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 24 '08 #2
When a hammer works, why by a pneumatic wrench?

I am not sure there is much overhead in LINQ, but performance is not the
only reason you use one programming construct over another. There is also
clarity of code (a maintainability issue). If you can use a simple if, I am
not sure iffing with LINQ is your best option.

LINQ is best when you have a set you need to filter, etc. It is great at
manipulating objects that conform to IEnumerable. While you can create
objects holding simple types, and then iterate, why add the extra complexity
to the code?

Just my two cents.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
"Mythran" <My*****@commun ity.nospamwrote in message
news:C1******** *************** ***********@mic rosoft.com...
Consider the following:

List<intints = new List<int>();
for (int i = 0; i < 20; i++) {
ints.Add(i);
}

// EXAMPLE 1
var strings =
from i in ints
where i 0 && i < 10 select i.ToString();

foreach (string s in strings) {
Console.WriteLi ne(s);
}

// EXAMPLE 2
foreach (int i in ints) {
if (i 0 && i < 10) {
Console.WriteLi ne(i.ToString() );
}
}
Now, what kind of overhead are we looking at for using LINQ in EXAMPLE 1
compared to using just the 'if' statement as shown in EXAMPLE 2? I have
multiple code blocks that I've redone to use LINQ and have started
thinking if I shouldn't have when a simple 'if' statement would do the
trick just fine....what do you peeps think?

Thanks a bunch!

Mythran

::I'm NOT a zero, I'm a one (just don't ask my wife)::

Oct 24 '08 #3
On Oct 24, 4:36*pm, "Mythran" <Myth...@commun ity.nospamwrote :
Consider the following:

List<intints = new List<int>();
for (int i = 0; i < 20; i++) {
* * * * ints.Add(i);
}
If you're only going to iterate through this, don't forget about
Enumerable.Rang e(0, 20)

<snip>
Now, what kind of overhead are we looking at for using LINQ in EXAMPLE 1
compared to using just the 'if' statement as shown in EXAMPLE 2? *I have
multiple code blocks that I've redone to use LINQ and have started thinking
if I shouldn't have when a simple 'if' statement would do the trick just
fine....what do you peeps think?
I think you should write the most readable code that gets the job
done, *but* test performance regularly, and have definite performance
goals. There's an overhead to LINQ to Objects, but it's not
particularly high. In most cases, it's not a problem. In a few cases,
it will be. Your profiler will help to tell you which is which :)

Jon
Oct 24 '08 #4
On Oct 24, 4:36*pm, "Mythran" <Myth...@commun ity.nospamwrote :
Consider the following:

List<intints = new List<int>();
for (int i = 0; i < 20; i++) {
* * * * ints.Add(i);
}
If you're only going to iterate through this, don't forget about
Enumerable.Rang e(0, 20)

<snip>
Now, what kind of overhead are we looking at for using LINQ in EXAMPLE 1
compared to using just the 'if' statement as shown in EXAMPLE 2? *I have
multiple code blocks that I've redone to use LINQ and have started thinking
if I shouldn't have when a simple 'if' statement would do the trick just
fine....what do you peeps think?
I think you should write the most readable code that gets the job
done, *but* test performance regularly, and have definite performance
goals. There's an overhead to LINQ to Objects, but it's not
particularly high. In most cases, it's not a problem. In a few cases,
it will be. Your profiler will help to tell you which is which :)

Jon
Oct 24 '08 #5


"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:b9******** *************** ***********@m3g 2000hsc.googleg roups.com...
On Oct 24, 4:36 pm, "Mythran" <Myth...@commun ity.nospamwrote :
>Consider the following:

List<intints = new List<int>();
for (int i = 0; i < 20; i++) {
ints.Add(i);
}

If you're only going to iterate through this, don't forget about
Enumerable.Rang e(0, 20)

<snip>
>Now, what kind of overhead are we looking at for using LINQ in EXAMPLE 1
compared to using just the 'if' statement as shown in EXAMPLE 2? I have
multiple code blocks that I've redone to use LINQ and have started
thinking
if I shouldn't have when a simple 'if' statement would do the trick just
fine....what do you peeps think?

I think you should write the most readable code that gets the job
done, *but* test performance regularly, and have definite performance
goals. There's an overhead to LINQ to Objects, but it's not
particularly high. In most cases, it's not a problem. In a few cases,
it will be. Your profiler will help to tell you which is which :)

Jon

This was just an example, and I would take a look at using the Range method
instead if this was real-world code....I just wanted to know that in
general, where a simple statement would work, why would I choose LINQ over
the simpler set....? Well, the answer that I've gathered from the replies
would be...I wouldn't. :)

Thanks,
Kip

Oct 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3601
by: =?Utf-8?B?V2lsc29uIEMuSy4gTmc=?= | last post by:
Hi Experts, I am doing a prototype of providing data access (read, write & search) through Web Service. We observed that the data storing in SQL Server 2005, the memory size is always within 250MB. Our aim is to support ~50K concurrency users. After investigation, we are thinking to use In-memory database for achieving
8
2632
by: Frank Calahan | last post by:
I've been looking at LINQ and it seems very nice to be able to make queries in code, but I use stored procs for efficiency. If LINQ to SQL only works with SQL Server and stored procs are more efficient, what use is LINQ to SQL, other than to have a simpler way to call my stored proc?
22
10382
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast. public List<ConferenceRoomOldWay(int minimumSeatingCapacity) {
4
1979
by: Jacek Jurkowski | last post by:
Why is it so slow? I really like that queries but using DataReader i have done my task's much more faster than ising LINQ...
4
2189
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and not reinsert it with a different revision number. Compounding the issue, we’ve also got an associated table storing properties for our entities which is not revisioned, but we still want changes to the children of our entity (additions, changes...
2
1735
by: Neil Chambers | last post by:
All, I have a class describing various actions to take against a LINQ to SQL datasource. What are the pros/cons of instantiating the LINQ object either in the root of the class (for lack of a better description) or within each of the methods? I am naturally drawn to instantiating the LINQ object in the root but some examples I've seen preffer to keep this local to the methods. Is there a compelling reason for this? It seems that garbage...
4
4728
by: shapper | last post by:
Hello, I have the following Linq query: var q = (from p in database.Posts join pt in database.PostsTags on p.PostID equals pt.PostID join t in database.Tags on pt.TagID equals t.TagID group t by p into pt select new PostPaper {
9
3955
by: imbirek8 | last post by:
Hi! I have some questions about Linq and transactions. In my opinion, when I do: DataClassesDataContext objDataClass = new DataClassesDataContext(connectionString); User user = new User() { UserName = "name" }; objDataClass.Users.InsertOnSubmit(user); objDataClass.SubmitChanges();
1
1968
by: Andy B | last post by:
I have to learn how to do one or the other: linq to sql or ado.net entity framework. There will be overhead no matter what way I go since I don't know either one. Which one would be the best one to use overall?
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.