Solved

LinQ performance vs LOOP/For Each

Posted on 2008-06-24
7
1,427 Views
Last Modified: 2013-11-07
Hi experts

i'm discovering LINQ and yes, this is very handy but what about performance?
it seems to have a huge impact on it?

may i use LinQ for normal use and For/Loop for performance (real time processing)

Can you tell me more?

Regards
0
Comment
Question by:Dnx_7
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:CMYScott
ID: 21860608
typically LINQ is actually quite fast - at VSLIve in Austin it was demostrated to be as-fast, or very, very close to that of a datareader - at least for data-access using Linq-To-Sql.

Can you post an example of your code which you believe suffers performance issues?
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 21860656
Sure !

                                For Each entryTrig As KeyValuePair(Of ChannelObj, DataTable) In _htTriggerLabel

                                    If String.Compare(entryTrig.Key.Name, event_channel) = 0 AndAlso xNode IsNot Nothing Then

                                        QueryLinQ = From r As DataRow In entryTrig.Value.AsEnumerable _
                                                    Where (eventTitle Like r.Field(Of String)("LABEL").TrimEnd AndAlso String.Compare(r.Field(Of String)("CATEGORYNAME").TrimEnd, eventCategory, True) = 0) _
                                                    Select r.Field(Of String)("MODENAME")

                                        If QueryLinQ.Count = 1 Then

                                            Me.DoActionPlaybox(QueryLinQ(0).TrimEnd, channelObject, entry.Value)

                                            'DoAction over, we can exit
                                            Exit Sub
.
                                        End If

                                    End If
                                Next


As you can see, i can change the LINQ query into a for each r as datarow in datatable.rows

i'm not an expert but seems that the query is more heavy (am i wrong?)
and the result.count seems to loop all the collection to get a count?!

so all of these makes processing heavier?

Kind Regards
0
 
LVL 11

Expert Comment

by:CMYScott
ID: 21860731
I'm guessing that the datarow/datatable is slowing you down dramatically.  

Where does your data originate?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Author Comment

by:Dnx_7
ID: 21860779
from SQL Server
the datatable is  refreshed every 5 minutes by another trigger process
that's why i don't query the database to speed up the process

so for you, i have to use LOOP against LINQ in this case (to gain Milliseconds)

regards
0
 
LVL 11

Accepted Solution

by:
CMYScott earned 500 total points
ID: 21861333
obviously I can't be sure without you testing it in your actual solution - but you're most likely losing more time by reading into the datatable than you would by using LINQ altogether..

if you keep your LINQ datacontext in scope, your trigger process would simply call the .Refresh method against the datacontext

that's not to say that you can't improve your for/loop logic and the linq query inside it to improve what you have - but I'd almost bet that you could improve the performance overall by eliminating the datatable if you can
0
 
LVL 11

Expert Comment

by:CMYScott
ID: 21861360
you might see some benefit by changing..

If String.Compare(entryTrig.Key.Name, event_channel) = 0 AndAlso xNode IsNot Nothing Then

to

If xNode IsNot Nothing AndAlso String.Compare(entryTrig.Key.Name,event_channel)=0 Then

since AndAlso will short-circuit, you want it to do the simpler evaluation first - in this case checking if xNode has a value is simply than doing the String.Compare.  The way you have it it's ALWAYS doing the String.Compare and only doing the xNode IsNot Nothing sometimes.

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question