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
Solved

LinQ performance vs LOOP/For Each

Posted on 2008-06-24
7
1,428 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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