none
Why LINQ here is performing very slow? RRS feed

  • Question

  • Do we have any better fastest approach other than having two nested for loops as shown below. LINQ here is performing very slow given below are the results.

    static void Main(string[] args)
            {
    Stopwatch w = new Stopwatch();
                w.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    CheckThePerson1(mytrade); 
                }
                w.Stop();
                Console.WriteLine("I time  = " + w.ElapsedTicks);
                Console.WriteLine("I time  = " + w.ElapsedMilliseconds);
                w.Reset();
                w.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    CheckThePerson2(mytrade); 
                }
                w.Stop();
                Console.WriteLine("II time = " + w.ElapsedTicks);
                Console.WriteLine("II time = " + w.ElapsedMilliseconds);
    }
    
    public static bool CheckThePerson1(Trade tradeObj)
            {
                List<PublicRecord> publicRecordList = tradeObj.PublicRecordList;
                for (int itr = 0; itr < publicRecordList.Count; itr++)
                {
                    if (publicRecordList[itr].Name == "66" || publicRecordList[itr].Name == "389")
                    {
                        return true;
                    }
                }
                return false;
            }
    
            public static bool CheckThePerson2(Trade tradeObj)
            {
                return tradeObj.PublicRecordList.Where(e => e.Name == "66" || e.Name == "389").Any();
            }
    
    Performance Ticks and Time:
    
    I time  = 6514224
    I time  = 2785
    
    II time = 11488985
    II time = 4913

    I even tried the below approach this is also proven very slow compare to for loop.

            public static bool CheckThePerson3(Trade tradeObj)
            {
                if (tradeObj.PublicRecordList.Where(e => e.Name == "66" || e.Name == "389").Count() > 0)
                    return true;
                else
                    return false;
            }

    Thank you,

    Ghouse



    Friday, July 13, 2012 7:25 AM

Answers

  • I'd assume that's the overhead of calling lambda methods and LINQ internals. for loops will be always faster in such cases.

    BTW, v2 and v3 are hardly any different.


    Miha Markic [MVP C#] http://blog.rthand.com

    • Proposed as answer by Joel Engineer Sunday, July 15, 2012 12:32 PM
    • Marked as answer by Ghouse Barq Monday, July 16, 2012 7:44 AM
    Sunday, July 15, 2012 11:00 AM

All replies

  • Do we have any better fastest approach other than having two nested for loops as shown below as LINQ here is performing very slow.

    static void Main(string[] args)
            {
    Stopwatch w = new Stopwatch();
                w.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    CheckThePerson1(mytrade); 
                }
                w.Stop();
                Console.WriteLine("I time  = " + w.ElapsedTicks);
                Console.WriteLine("I time  = " + w.ElapsedMilliseconds);
                w.Reset();
                w.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    CheckThePerson2(mytrade); 
                }
                w.Stop();
                Console.WriteLine("II time = " + w.ElapsedTicks);
                Console.WriteLine("II time = " + w.ElapsedMilliseconds);
    }
    
    public static bool CheckThePerson1(Trade tradeObj)
            {
                List<PublicRecord> footNoteList = tradeObj.PublicRecordList;
                for (int itr = 0; itr < footNoteList.Count; itr++)
                {
                    if (footNoteList[itr].Name == "66" || footNoteList[itr].Name == "389")
                    {
                        return true;
                    }
                }
                return false;
            }
    
            public static bool CheckThePerson2(Trade tradeObj)
            {
                return tradeObj.PublicRecordList.Where(e => e.Name == "66" || e.Name == "389").Any();
            }
    
    Performance Ticks and Time:
    
    I time  = 6514224
    I time  = 2785
    
    II time = 11488985
    II time = 4913
    

    I even tried the below approach this is also proven very slow compare to for loop.

    public static bool CheckThePerson3(Trade tradeObj) { if (tradeObj.PublicRecordList.Where(e => e.Name == "66" || e.Name == "389").Count() > 0) return true; else return false; }

    and even this:

    public static bool CheckThePerson2(Trade tradeObj)
            {
                if (tradeObj.PublicRecordList.FindAll(e => e.Name == "66" || e.Name == "389").Count() > 0)
                    return true;
                else
                    return false;
            }

     

    Thank you,

    Ghouse

    Friday, July 13, 2012 7:44 AM
  • try changing the statement

    List<PublicRecord> publicRecordList = tradeObj.PublicRecordList; to

    List<PublicRecord> publicRecordList = tradeObj.PublicRecordList.ToList();


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, July 13, 2012 8:15 AM
  • http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5085624a-0773-430e-b2a6-bc8f405f9dc0

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, July 13, 2012 8:19 AM
  • I'd assume that's the overhead of calling lambda methods and LINQ internals. for loops will be always faster in such cases.

    BTW, v2 and v3 are hardly any different.


    Miha Markic [MVP C#] http://blog.rthand.com

    • Proposed as answer by Joel Engineer Sunday, July 15, 2012 12:32 PM
    • Marked as answer by Ghouse Barq Monday, July 16, 2012 7:44 AM
    Sunday, July 15, 2012 11:00 AM
  • This might run faster. It stops on the first match instead of passing the entire list.

    return tradeObj.PublicRecordList.Any(e => e.Name == "66" || e.Name == "389");
    

    Tuesday, July 17, 2012 6:16 PM