?
Solved

LinQ performance vs LOOP/For Each

Posted on 2008-06-24
7
Medium Priority
?
1,435 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
6 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

807 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