473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.WriteLine(s);
}

// EXAMPLE 2
foreach (int i in ints) {
if (i 0 && i < 10) {
Console.WriteLine(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 2579
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*****@community.nospamwrote in message
news:C1**********************************@microsof t.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.WriteLine(s);
}

// EXAMPLE 2
foreach (int i in ints) {
if (i 0 && i < 10) {
Console.WriteLine(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...@community.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.Range(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...@community.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.Range(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.comwrote in message
news:b9**********************************@m3g2000h sc.googlegroups.com...
On Oct 24, 4:36 pm, "Mythran" <Myth...@community.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.Range(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
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...
8
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...
22
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....
4
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
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...
2
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...
4
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...
9
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() { ...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.