Why is the performance of LINQ to XML superior to that of XmlDocument ?, Linqxmldocument

Source: Internet
Author: User

Why is the performance of LINQ to XML superior to that of XmlDocument ?, Linqxmldocument

I 've been very busy. I 've pressed a lot of posts. I'll post one today. Let's see your mood later.

Today, someone in the group asked me how to parse web. config for convenience. Then I recommended Linq to XML, and then someone said,"I would rather XmlDocument and SeleteNodes and SeleteNode", Do not use a series of frameworks such as LINQ, or even deny EntityFramework, and think that these are all so-called" lazy technology "at the cost of performance. I want to declare one thing here,There is no right to speak without testing. Not all "lazy technologies" are at the cost of performance.. I liked technology and did not like arbitrary comments, so I started the discussion. This article is just a summary.

Test the performance of LINQ to XML

Many of you have already performed performance tests. I will not repeat them, as shown in the following link:

Performance Comparison of XML data reading methods (1)

Performance Comparison of XML data reading methods (II)

From the above results, we cannot see that the performance of Linq to Xml is obviously better than that of XmlDocument. I like tracing. If I do this, there will always be various paradox, such:


[Codeshuai] -------- 13:52:01
Are you sure you want to fix this?
[Conu] -------- 13:52:32
What height?
[Codeshuai] -------- 13:52:42
Why do the two above have the Add
[Codeshuai] -------- 13:52:49
None of the following
[Codeshuai] -------- 13:52:54
Is this test fair?
[Codeshuai] -------- 13:53:18
For example, a new Object []
[Codeshuai] -------- 13:54:38
Four tests have problems.
[Codeshuai] -------- 13:56:03
Add is available for more than two 40 seconds.

In fact, none of his problems have been solved. This is not an issue of Add at all. The ToList () method of Linq must have done the same thing. If you suspect it, you can write a test on your own. So I think it is necessary to explain why the performance of LINQ to XML is better than that of XmlDocument.

Why is the performance of LINQ to XML superior to that of XmlDocument?

First, we need to understand the following:

A good mother, XmlReader, has been added to the LINQ to XML file.

InXmlReaderBased on the implementation, that is, LINQ to XML is derived from XmlReader, which is higher than XmlReader.

Genetics is very important!

XmlReader is a fast non-Cache analyzer. Access to the XML data stream is read-only.

 

Secondly, there is a good father-in-writing-to-XML.

One of the most important performance advantages of LINQ to XML (compared with XmlDocument) Is that queries in LINQ to XML are statically compiled, while XPath queries must be interpreted at runtime.

This factor is crucial to performance. The so-called "sub-Teacher, father-to-Father"!

That is to say, the query of LINQ to XML is compiled into static link method calls, which greatly improves the performance. In contrast, XmlDocument must perform the following operations internally each time the SelectNodes method is called:

This requires a lot of work compared to the work completed by the corresponding LINQ to XML query.

In addition, LINQ to XML inherits the father'sDelayed executionCan also improve the performance.

As a father, XmlDocument cannot be compared.

Therefore, the starting points of the rich generation and the official generation are higher than yours. If you do not pay N times more effort than they do, you cannot even reach their starting points.

 

Knowledge about delayed execution in popular science:

Delayed execution means that the expression computing is delayed until the actual implementation value is needed. When large data sets must be operated, especially in programs that contain a series of link queries or operations, delayed execution can greatly improve performance. Under optimal conditions, delayed execution only allows single loop access to the source set.
The LINQ technology is widely used for delayed execution, including in members of the core System. Linq class and extension methods (such as System. Xml. LINQ. Extensions) in different Linq namespaces.

 

In addition to the above, there are also some of the advantages of his own improvement in the growth process, such:XName and XNamespace objects are atomic. If the two objects contain the same name, they reference the same object. That is to say, when comparing whether two atomic names are equal, you only need to determine whether these two references point to the same object without having to perform a "time-consuming" string comparison, which is helpful for performance improvement.

 

Conclusion

Although this is not a movie, it is still necessary to come to an end.


A problem with linq to xml

Var result = e. Elements ("ds ")
. Where (ds => ds. Element ("ClassID"). Value = "64") // obtain the selected Element 1.
. Select (ds => ds. Element ("arrChildID"). Value. Split (',') // pre-Prepare the ID array 2.
. Select (arrid => e. Elements ("ds ")
. Where (ds2 => arrid. Contains (ds2.Element ("ClassID"). Value) // This sub-process is executed once because "1" actually has only one result.
. SelectMany (ds2 => ds2); // combines the result set.

// Tested

When you read xml using linq to xml, when you start to traverse the data set, the variable is clearly defined, or the current context does not have an exception

Var par = from settings in XElement. Load ("monitorSettings. xml"). Elements ("rootpath") select settings;
No data is read.
 

Related Article

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.