The performance of System.Xml - Insert operations
In this post, I will analyse the ways that you can add nodes to a XmlDocument and wich way is the fastest, using the .NET framework 1.1.
How can we add nodes to a XmlDocument?
There are four ways for you to add nodes to a XmlDocument:
- XmlDocument.InsertAfter
- XmlDocument.InsertBefore
- XmlDocument.Append
- XmlDocument.PrependChild
So, wich method is the fastest?
To answer that question I’ve created a test program that uses the content of a xml file (11KB) to create a XmlDocument in memory. Then the user can choose wich 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 this issues into account.
My performance test uses the following approach:
- There are 2 threads. One thread is sleeping for a certaing 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.
- The number of times that the method was invoked is registered in a variable.
- 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.
- 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.
The results
- AppendChild - 38ns
- InsertAfter - 12ns
- InsertBefore - 13ns
- PrependChild - 14 ns
Conclusions
The InsertAfter, InsertBefore and PrependChild methods have basicly the same performance results. The AppendChild is a little slower.
One interesting conclusion is the diference 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 begining of the list of child nodes that to the end.
The challenge
This tests were made with a 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 this tests and post the here results.
Here’s the source code or get directly the exe and the xml file used in the tests here.
Remenber to put the xml file in the same folder that the exe file.
Hope that you can improve your code with this performance test.

