The legendary LINQ in the LINQ Performance Analysis series

Source: Internet
Author: User
Tags abs

The discussion about the. Net Framework 3.5 has been a real buzz since Beta1 was released. I am probably the one who eats crabs earlier, and Beta1 it for formal development at the very beginning. Although many bugs have been encountered during this period, I personally feel quite satisfied with the core features of the. Net Framework 3.5-LINQ. However, the only cool syntax for LINQ is not enough to determine the technology used in the project. In fact, if it wasn't for my personal-style development, it would be important to assess whether the new technology was performing satisfactorily. If you are not aware of LINQ, you can make perceptual perceptions with simple VB9 examples.

LINQ Basics Sample

Dim Collection As String() = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Shijiazhuang", "Tianjin", "Taiyuan}
Console.WriteLine((From City In Collection Where City(0) = "T").First)

One thing to emphasize here is that you never confuse LINQ with LINQ to SQL (DLinq), DLinq is just an application of LINQ, not all of LINQ. With LINQ comes a lot of compiler features, both C#3 and VB9, with many refreshing new syntax features. However, because the. NET Framework 3.x is a superset of the. NET Framework 2, all syntax features are just translations at compile time. If you attempt to decompile a LINQ expression, a Lambda or an anonymous function, you will see a large number of function nesting, unknown functions and other confusing brain-swelling program structures. All this is done by the compiler. You don't have to worry about Var in C #, or the loading/unpacking problem with the definition of as variables in VB, which will be inferred as real data types in IL.

When discussing the performance issues of LINQ, we will not only be limited to simple time comparisons, we will also make a simple analysis of LINQ implementation code and discuss ways to optimize database performance on the T-SQL statements generated by LINQ to. 工欲善其事, its prerequisite, the platform tested in this series is based on SQL Server SP2, and the Decompile tool uses Lutz Roeder ' s Reflector.

Anyway Because the extension method is applied, LINQ is able to query the contents of any existing IEnumerable (of T). However, performance problems are hidden under versatility and ease of use. Let's do a short test that uses LINQ to get the beginning, end, and total of a random number array, and compare it to the traditional way of getting an array index. The test code is as follows:

Visual Basic 9-linq Performance test: getting random array data

 Sub Main () 
Dim Start As Integer
for Count As Integer = 0 to 2
Dim Random As New Random
Dim Temparray as New List (of Integer)
' generate test array
for I as Integer = 0 to 50000
Temparray.add (Random.next (10000, 99999))
Next
Test Query
Dim Source as Integer () = Temparray.toarray
Calculated time
to get the last
Start = timer
Dim last As Integer, the As Integer, Length as Integer
for I As Integer = 0 to 500000
last = source.last
i = source.first
Length = Source.count
Next
System.Console.WriteLine ("The first end of a Linq query array is time-consuming:" & Math.Abs (Timer-start) * 1000 & "milliseconds") ' Method 2: Calculates the time
Start = Timer
' Gets the last
Length = source.length-1
For I as IntEger = 0 to 500000
last = Source (Length-1)
i = Source (0)
Le Ngth = source.length
Next
System.Console.WriteLine ("The end of a traditional query array and time consuming:" & Math.Abs (Timer- Start) * 1000 & "millisecond")
Next
End Sub

The program runs in release mode and tests 3 consecutive times. The test results are as follows:

Linq query array at the end of the first and the statistics time consuming: 2109.375 ms

Traditional query array at the end of the first and the statistics time consuming: 296.875 milliseconds

Linq query array at the end of the first and the statistics time consuming: 2406.25 ms

Traditional query array at the end of the first and the statistics time consuming: 406.25 milliseconds

Linq query array at the end of the first and the statistics time consuming: 2156.25 ms

Traditional query array at the end of the first and the statistics time consuming: 218.75 milliseconds

See here, the gap between common LINQ and traditional methods is clear. Although it is just a simple test of glimpse, if you are not necessary, you should try to use the original array of functionality. Of course, you reader want to know why the gap is so great and listen to let's. Limited level, if there is flawed, please understand and point out.

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.