Solved

LinQ performance vs LOOP/For Each

Posted on 2008-06-24
7
1,425 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now