Can I use a series of (I) Comparison of the Efficiency of LINQ to Object?

Source: Internet
Author: User
Preface

Description: Language INtegrated Query is a set of extensions for c # and Visual Basic languages.

Category: LINQ to Object, LINQ to XML, LINQ to SQL, LINQ to DataSet, and LINQ to ADO. NET.

Related: I believe that everyone is familiar with the linq, if not familiar with, you can refer to the MSDN address: http://msdn.microsoft.com/zh-cn/library/bb397933.aspx
Reason: Can I use it? In many cases, there are different opinions. Some people are confused, some people wait and see, some people think it doesn't matter, or some people say it's just a syntactic sugar, which is useless, which of the following are the mysteries of our series. First, let's take a look at the Efficiency Test of LINQ to Object in array filtering.

Instance analysis

Test environment: visual studio 2011 Beta (netframework 4.0 +) C # console Program

Test Requirement: Query more than 10000000 of the 100 data records.

Core code (LINQ ):

var linqList = from num in list1                where num > 100                select num;

Complete code:

/// <Summary> /// Efficiency Test /// </summary> /// <param name = "testCount"> Number of tests </param> private static void timeTest (int testCount) {const int listCount = 10000000; // array length Random random = new Random (); // random data build value // array build List <int> listData = new List <int> (); for (int I = 0; I <listCount; I ++) {listData. add (random. next (10000);} // LINQ test Stopwatch linq_Stopwatch = new Stopwatch (); linq_Stopwatch.Start (); Var linqList = from num in listData where num> 100 select num; var linqCount = linqList. count (); linq_Stopwatch.Stop (); // test Stopwatch before_Stopwatch = new Stopwatch (); before_Stopwatch.Start (); List <int> beforeList = new List <int> (listCount) in normal mode ); for (int I = 0; I <listData. count (); I ++) {if (listData [I]> 100) beforeList. add (listData [I]);} var beforeCount = beforeList. count; before_Stopw Atch. Stop (); // print the result Console. WriteLine (String. Format ("tested at {0}, test: {5} pieces of data. \ N \ r \ t: {1} millisecond for LINQ. {2} data records are filtered. \ N \ r \ t normal time: {3} milliseconds, filtered {4} data records. \ R \ n ", testCount, linq_Stopwatch.ElapsedMilliseconds, linqCount, before_Stopwatch.ElapsedMilliseconds, beforeCount, listCount ));}

 

Result

 

Conclusion: it can be seen that the efficiency of processing data by using LINQ to Object is better than that of manual judgment. Of course, the greatness of LINQ should be her concise and elegant syntax. When processing data, I personally recommend using LINQ to Object!

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.