Click here to Skip to main content
12,955,118 members (57,996 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 1 Sep 2006

The Performance of System.Xml - Insert Operations

, 1 Sep 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
The results of some perfomance tests of insert-like operations of System.Xml
Sample Image - XmlInsertPerfomanceTest.jpg


In this post, I will analyze the ways in which you can add nodes to an XmlDocument and discuss which way is the fastest, using the .NET 1.1 Framework.

How Can We Add Nodes to an XmlDocument?

There are four ways for you to add nodes to an XmlDocument:

  • XmlDocument.InsertAfter
  • XmlDocument.InsertBefore
  • XmlDocument.Append
  • XmlDocument.PrependChild

So, which method is the fastest?

To answer that question, I've created a test program that uses the content of an XML file (11KB) to create an XmlDocument in memory. Then the user can choose which method he wants to test. The results are measured in nanoseconds.

How Did You Make the Performance Test?

One could easily say that the easiest way to make a performance test is something like this:

DateTime before = DateTime.Now;
// do the test...
DateTime res = before.Subtract(DateTime.Now);

Unfortunately, this code forgets that there are threads running in your system. Our performance test application isn't running all alone and most important, it isn't running all the time.

Hence, a good performance test must take these issues into account.

My performance test uses the following approach:

  1. There are two threads. One thread is sleeping for a certain period of time. I like to call it the TimerThread. While it's sleeping, the other thread is invoking the method to test, over and over again, until the first thread wakes up.
  2. The number of times that the method was invoked is registered in a variable.
  3. The TimeThread executes the following code: (timeThatWasSleeping*1000000)/counter. The counter variable holds the number of times that the method to be tested was called.
  4. The time in nanoseconds that the TimerThread calculates is the time that the method takes to finish.

For minimizing the scheduler interference in the tests, I've give ThreadPriority.Highest to the thread that is making the test. However if you run the tests multiple times, you can get different values because of this.

Some Test Code

Let's look at some code, shall we?

The application is configured using some static variables.

static int counter = 0;            // the number of times that the method was called
static bool run = false;           // the test thread is running?
static int timeMilisec = 5000;     // For how long do the test run?
static XmlDocument xmlDoc = new XmlDocument();
static object mon = new object();  // Object used for synchronization
static Thread thTimer;

The Main() method is responsible for setting up the "test thread" and the "timer thread" based on the test that the user chose.

static void Main(string[] args)
    string cmd = String.Empty;
            Monitor.Wait(mon);// wait for the test to end.
        Console.WriteLine("XmlDocument.AppendChild: a");
        Console.WriteLine("XmlDocument.InsertAfter: b");
        Console.WriteLine("XmlDocument.InsertBefore: c");
        Console.WriteLine("XmlDocument.PrependChild: d");
        Console.WriteLine("Quit: q");
        Console.Write("Enter your choice:");
        cmd = Console.ReadLine();
        using (StreamReader sr = new StreamReader("news.xml")) 
        xmlDoc.LoadXml( sr.ReadToEnd() );                
        run = true;

        Thread thw = null;
            case "a":
                thw = new Thread(new ThreadStart(TestXmlDocAppendChild));
            case "b":
                thw = new Thread(new ThreadStart(TestXmlDocInsertAfter));
            case "c":
                thw = new Thread(new ThreadStart(TestXmlDocInsertBefore));
            case "d":
                thw = new Thread(new ThreadStart(TestXmlDocPrependChild));
            case "q":
        Console.WriteLine("\nTest started...");
        thTimer = new Thread(new ThreadStart(ThreadTimer));
        thw.Priority = System.Threading.ThreadPriority.Highest;

For example, the thread that is testing the AppendChild method tries to call that method the maximum number of times.

private static void TestXmlDocAppendChild()

The timer thread executes the following code:

private static void ThreadTimer()
    run = false;
    Console.WriteLine("Result: "+(timeMilisec*1000000)/counter + "ns\n\n");
    counter = 0;

The Results

  • AppendChild - 38ns
  • InsertAfter - 12ns
  • InsertBefore - 13ns
  • PrependChild - 14ns


The InsertAfter, InsertBefore and PrependChild methods have basically the same performance results. The AppendChild is a little slower.

One interesting conclusion is the difference between the AppendChild and the PrependChild. The first "adds the specified node to the end of the list of child nodes, of this node" and the second "Adds the specified node to the beginning of the list of child nodes for this node." Hence, it's faster to insert nodes to the beginning of the list of child nodes than to the end.

The Challenge

This tests were made with an Intel Pentium M processor 1500Mhz and 512 MB of RAM running Windows XP Pro. I would like to challenge everyone who would like to run these tests to post the results here.

Hope that you can improve your code with this performance test.


  • 1st September, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Advantis
Portugal Portugal
Bruno Coelho is a Software Engineer from Portugal.

He's been working with .NET since version 1.1 to 3.5, using the hard won skills at ISEL, where he has got a degree in Informatic Engineering, to design critical applications using the best practices and design patterns in the industry.

He's current interests are: Performance & Best practices and Design patterns.

You may also be interested in...

Comments and Discussions

GeneralResults Pin
Dilatazu9-Jul-07 7:09
memberDilatazu9-Jul-07 7:09 
GeneralMeasurment Principles Pin
Alois Kraus13-Oct-06 4:36
memberAlois Kraus13-Oct-06 4:36 
GeneralTime calculation Pin
carlopagliei5-Sep-06 23:48
membercarlopagliei5-Sep-06 23:48 
GeneralRe: Time calculation Pin
Bcoelho6-Sep-06 2:10
memberBcoelho6-Sep-06 2:10 
GeneralRe: Time calculation Pin
carlopagliei6-Sep-06 5:36
membercarlopagliei6-Sep-06 5:36 
AnswerRe: Time calculation Pin
Bcoelho7-Sep-06 8:10
memberBcoelho7-Sep-06 8:10 
GeneralRe: Time calculation Pin
carlopagliei7-Sep-06 21:15
membercarlopagliei7-Sep-06 21:15 
QuestionI fail to see how your approach solve the problem ... Pin
Sébastien Lorion5-Sep-06 10:11
memberSébastien Lorion5-Sep-06 10:11 
AnswerRe: I fail to see how your approach solve the problem ... Pin
Bcoelho6-Sep-06 2:15
memberBcoelho6-Sep-06 2:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 1 Sep 2006
Article Copyright 2006 by Bcoelho2000
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid