Share via


Preatomization of XName Objects

Introduction

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOCOne approach that you can take to gain some performance is to pre-atomize XName objects. This would yield performance benefits when creating a large XML tree where specific names are repeated. The approach is to declare and initialize XName objects before the construction of the XML tree, and then to use the XName objects rather than specifying strings for the element and attribute names. This technique can yield significant performance gains if you are creating a large number of elements (or attributes) with the same name.

You should profile this technique for your scenario to decide if you should use it.

Example

The following example demonstrates this.

[c#]

XName Root = "Root";

XName Data = "Data";

XName ID = "ID";

 

XElement root = new XElement(Root,

    new XElement(Data,

        new XAttribute(ID, "1"),

        "4,100,000"),

    new XElement(Data,

        new XAttribute(ID, "2"),

        "3,700,000"),

    new XElement(Data,

        new XAttribute(ID, "3"),

        "1,150,000")

);

 

Console.WriteLine(root);

 

This example produces the following output:

 

[xml]

<Root>

  <Data ID="1">4,100,000</Data>

  <Data ID="2">3,700,000</Data>

  <Data ID="3">1,150,000</Data>

</Root>

 

The following example shows the same technique where the XML document is in a namespace:

[c#]

XNamespace aw = "https://www.adventure-works.com";

XName Root = aw + "Root";

XName Data = aw + "Data";

XName ID = "ID";

 

XElement root = new XElement(Root,

    new XAttribute(XNamespace.Xmlns + "aw", aw),

    new XElement(Data,

        new XAttribute(ID, "1"),

        "4,100,000"),

    new XElement(Data,

        new XAttribute(ID, "2"),

        "3,700,000"),

    new XElement(Data,

        new XAttribute(ID, "3"),

        "1,150,000")

);

 

Console.WriteLine(root);

 

This example produces the following output:

[xml]

<aw:Root xmlns:aw="https://www.adventure-works.com">

  <aw:Data ID="1">4,100,000</aw:Data>

  <aw:Data ID="2">3,700,000</aw:Data>

  <aw:Data ID="3">1,150,000</aw:Data>

</aw:Root>

 

A more pertinent example is one where the content of the element is supplied by a query:

[c#]

XName Root = "Root";

XName Data = "Data";

XName ID = "ID";

 

DateTime t1 = DateTime.Now;

XElement root = new XElement(Root,

    from i in System.Linq.Enumerable.Range(1, 100000)

    select new XElement(Data,

        new XAttribute(ID, i),

        i * 5)

);

DateTime t2 = DateTime.Now;

 

Console.WriteLine("Time to construct:{0}", t2 - t1);

 

The above example performs better than the following, where names are not pre-atomized:

 

[c#]

DateTime t1 = DateTime.Now;

XElement root = new XElement("Root",

    from i in System.Linq.Enumerable.Range(1, 100000)

    select new XElement("Data",

        new XAttribute("ID", i),

        i * 5)

);

DateTime t2 = DateTime.Now;

 

Console.WriteLine("Time to construct:{0}", t2 - t1);

Comments

  • Anonymous
    June 12, 2008
    It can be problematic for us here at Microsoft to make specific performance claims of one technology