// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Xml; namespace E { public class Program { public static List Parse1(XmlElement elm) { List res = new List(); for(int i = 0; i < elm.ChildNodes.Count; i++) { res.Add(elm.ChildNodes[i].Name); } return res; } public static List Parse2(XmlElement elm) { List res = new List(); XmlNodeList childs = elm.ChildNodes; for(int i = 0; i < childs.Count; i++) { res.Add(childs[i].Name); } return res; } public static List Parse3(XmlElement elm) { List res = new List(); XmlNode n = elm.FirstChild; while(n != null) { res.Add(n.Name); n = n.NextSibling; } return res; } public static List Parse4(XmlElement elm) { List res = new List(); foreach(XmlNode n in elm.ChildNodes) { res.Add(n.Name); } return res; } private const int N = 10000; private const int REP = 5; private const int SCALE = 100; public static void Main(string[] args) { XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateElement("all")); for(int i = 0; i < N ; i++) { doc.DocumentElement.AppendChild(doc.CreateElement("one")); } DateTime dt1 = DateTime.Now; for(int j = 0; j < REP; j++) { if(Parse1(doc.DocumentElement).Count != N) { throw new Exception("Ooops"); } } DateTime dt2 = DateTime.Now; Console.WriteLine("1: " + (dt2 - dt1).TotalSeconds); DateTime dt3 = DateTime.Now; for(int j = 0; j < REP; j++) { if(Parse2(doc.DocumentElement).Count != N) { throw new Exception("Ooops"); } } DateTime dt4 = DateTime.Now; Console.WriteLine("2: " + (dt4 - dt3).TotalSeconds); DateTime dt5 = DateTime.Now; for(int j = 0; j < REP * SCALE; j++) { if(Parse3(doc.DocumentElement).Count != N) { throw new Exception("Ooops"); } } DateTime dt6 = DateTime.Now; Console.WriteLine("3: " + (dt6 - dt5).TotalSeconds / SCALE); DateTime dt7 = DateTime.Now; for(int j = 0; j < REP * SCALE; j++) { if(Parse4(doc.DocumentElement).Count != N) { throw new Exception("Ooops"); } } DateTime dt8 = DateTime.Now; Console.WriteLine("4: " + (dt8 - dt7).TotalSeconds / SCALE); } } }