about performance in linq query

T

Tony Johansson

Hello!

Assume I want to delete an object in a collection.
I don't have the object that should be removed but I have the fields where I
can loop through
the collection until I find the right object and when I find the right
object I delete it and do a break from the loop.

Now to my question what gives the best performace of these two alternatives.
1. Do what I do know loop through the collection until I find the right
object and then delete it and then do a break from the looping.
2. Use a linq query do find the object to be deletet and the delete this
object directly without looping.

//Tony
 
G

Göran Andersson

Tony said:
Hello!

Assume I want to delete an object in a collection.
I don't have the object that should be removed but I have the fields
where I can loop through
the collection until I find the right object and when I find the right
object I delete it and do a break from the loop.

Now to my question what gives the best performace of these two
alternatives.
1. Do what I do know loop through the collection until I find the right
object and then delete it and then do a break from the looping.
2. Use a linq query do find the object to be deletet and the delete this
object directly without looping.

//Tony

There is no real difference, the LINQ query will also loop through the
collection.

The best for performance would be to get the index of the item to delete
instead of a reference to the item. That way the code that deletes the
item doesn't have to loop through the collection once more to find the
item to delete.
 
T

Tom P.

There is no real difference, the LINQ query will also loop through the
collection.

The best for performance would be to get the index of the item to delete
instead of a reference to the item. That way the code that deletes the
item doesn't have to loop through the collection once more to find the
item to delete.

I had always understood that LINQ was faster than (n-1) loops. Where
can I find more information about this?

What good is LINQ for Objects if it's the same as (n-1)?

Tom P.
 
G

Göran Andersson

Tom said:
I had always understood that LINQ was faster than (n-1) loops.

How would LINQ look at each item without looping through them? It's new
and cool, but it's not magical...
Where
can I find more information about this?

Hm... how might one find information... ;)

http://www.google.com/search?q=linq+performance


For example:


http://davepeck.org/linq-collection-performance/

"Even in simple cases, LINQ over collections is roughly 2.5X slower than
old-style C# 2 code."

(I think that LINQ has improved since the article was written, though.
From what I have seen it's almost as fast as the older code.)


http://codebetter.com/blogs/steve.h...rganization-to-where-clause-optimization.aspx

"That’s eye popping – it’s more than two factors of 10 faster than the
best optimized Linq query"

(Not sure that this is a representative test, but you surely risk
getting results like that...)

What good is LINQ for Objects if it's the same as (n-1)?

For simple iterations, not much.
 
P

Pavel Minaev

I had always understood that LINQ was faster than (n-1) loops. Where
can I find more information about this?

LINQ is not magic. If all you have is a plain non-sorted, non-indexed
collection, then there's no way to do anything faster than linear scan
with it.
What good is LINQ for Objects if it's the same as (n-1)?

It's shorter, and it's clearer what you're trying to achieve. A
foreach loop can serve many purposes, only one of which is to find a
specific element in a collection. Because of that, you have to
actually read the loop body in some details, and parse it fully to
understand what it does. With something like list.First(o => ...), it
is immediately evident what the expression as a whole does. It doesn't
need any temporary variables to store the result, either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top