Solved

LinQ performance vs LOOP/For Each

Posted on 2008-06-24
7
1,430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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