Which one is "quicker" ?

This is a discussion on Which one is "quicker" ? within the C# Programming forums, part of the General Programming Boards category; I need to use some field "ID" a couple of times in loop, say for example 4 times in a ...

  1. #1
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74

    Which one is "quicker", using a string or SelectSingleNode? ?

    I need to use some field "ID" a couple of times in loop, say for example 4 times in a loop, which one of these code would be faster ?

    Code:
    foreach(XmlNode XMLNODE in XML_menu.SelectSingleNode("menu").ChildNodes){
    Console.WriteLine( XMLNODE.SelectSingleNode("@id").InnerText );
    Console.WriteLine( XMLNODE.SelectSingleNode("@id").InnerText );
    Console.WriteLine( XMLNODE.SelectSingleNode("@id").InnerText );
    Console.WriteLine( XMLNODE.SelectSingleNode("@id").InnerText );
    }
    or

    Code:
    foreach(XmlNode XMLNODE in XML_menu.SelectSingleNode("menu").ChildNodes){
    string ID = XMLNODE.SelectSingleNode("@id").InnerText;
    Console.WriteLine( ID );
    Console.WriteLine( ID );
    Console.WriteLine( ID );
    Console.WriteLine( ID );
    }
    What I mean is, is it faster to you "XMLNODE.SelectSingleNode" many times or should I first get its value into some string and then use that string many times?
    Last edited by AloneInTheDark; 02-08-2008 at 06:41 PM. Reason: Altered the title.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    You should benchmark it if you're really interested in this, but...

    While it is possible that the JIT compiler will optimize this at run-time, based on looking at the IL it appears that the second is likely quicker (which is what I would have suspected without optimization). There was no difference I could find between Debug and Release mode (optimized) code either.

    First method:
    Code:
    .method private hidebysig instance void Test1(class [System.Xml]System.Xml.XmlDocument doc) cil managed
    {
        .maxstack 2
        .locals init (
            [0] class [System.Xml]System.Xml.XmlNode node,
            [1] class [mscorlib]System.Collections.IEnumerator CS$5$0000,
            [2] bool CS$4$0001,
            [3] class [mscorlib]System.IDisposable CS$0$0002)
        L_0000: nop 
        L_0001: nop 
        L_0002: ldarg.1 
        L_0003: ldstr "menu"
        L_0008: callvirt instance class [System.Xml]System.Xml.XmlNode [System.Xml]System.Xml.XmlNode::SelectSingleNode(string)
        L_000d: callvirt instance class [System.Xml]System.Xml.XmlNodeList [System.Xml]System.Xml.XmlNode::get_ChildNodes()
        L_0012: callvirt instance class [mscorlib]System.Collections.IEnumerator [System.Xml]System.Xml.XmlNodeList::GetEnumerator()
        L_0017: stloc.1 
        L_0018: br.s L_0080
        L_001a: ldloc.1 
        L_001b: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()
        L_0020: castclass [System.Xml]System.Xml.XmlNode
        L_0025: stloc.0 
        L_0026: nop 
        L_0027: ldloc.0 
        L_0028: ldstr "@id"
        L_002d: callvirt instance class [System.Xml]System.Xml.XmlNode [System.Xml]System.Xml.XmlNode::SelectSingleNode(string)
        L_0032: callvirt instance string [System.Xml]System.Xml.XmlNode::get_InnerText()
        L_0037: call void [mscorlib]System.Console::WriteLine(string)
        L_003c: nop 
        L_003d: ldloc.0 
        L_003e: ldstr "@id"
        L_0043: callvirt instance class [System.Xml]System.Xml.XmlNode [System.Xml]System.Xml.XmlNode::SelectSingleNode(string)
        L_0048: callvirt instance string [System.Xml]System.Xml.XmlNode::get_InnerText()
        L_004d: call void [mscorlib]System.Console::WriteLine(string)
        L_0052: nop 
        L_0053: ldloc.0 
        L_0054: ldstr "@id"
        L_0059: callvirt instance class [System.Xml]System.Xml.XmlNode [System.Xml]System.Xml.XmlNode::SelectSingleNode(string)
        L_005e: callvirt instance string [System.Xml]System.Xml.XmlNode::get_InnerText()
        L_0063: call void [mscorlib]System.Console::WriteLine(string)
        L_0068: nop 
        L_0069: ldloc.0 
        L_006a: ldstr "@id"
        L_006f: callvirt instance class [System.Xml]System.Xml.XmlNode [System.Xml]System.Xml.XmlNode::SelectSingleNode(string)
        L_0074: callvirt instance string [System.Xml]System.Xml.XmlNode::get_InnerText()
        L_0079: call void [mscorlib]System.Console::WriteLine(string)
        L_007e: nop 
        L_007f: nop 
        L_0080: ldloc.1 
        L_0081: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
        L_0086: stloc.2 
        L_0087: ldloc.2 
        L_0088: brtrue.s L_001a
        L_008a: leave.s L_00a3
        L_008c: ldloc.1 
        L_008d: isinst [mscorlib]System.IDisposable
        L_0092: stloc.3 
        L_0093: ldloc.3 
        L_0094: ldnull 
        L_0095: ceq 
        L_0097: stloc.2 
        L_0098: ldloc.2 
        L_0099: brtrue.s L_00a2
        L_009b: ldloc.3 
        L_009c: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        L_00a1: nop 
        L_00a2: endfinally 
        L_00a3: nop 
        L_00a4: ret 
        .try L_0018 to L_008c finally handler L_008c to L_00a3
    }
    Second method:
    Code:
    method private hidebysig instance void Test2(class [System.Xml]System.Xml.XmlDocument doc) cil managed
    {
        .maxstack 2
        .locals init (
            [0] class [System.Xml]System.Xml.XmlNode node,
            [1] string text,
            [2] class [mscorlib]System.Collections.IEnumerator CS$5$0000,
            [3] bool CS$4$0001,
            [4] class [mscorlib]System.IDisposable CS$0$0002)
        L_0000: nop 
        L_0001: nop 
        L_0002: ldarg.1 
        L_0003: ldstr "menu"
        L_0008: callvirt instance class [System.Xml]System.Xml.XmlNode [System.Xml]System.Xml.XmlNode::SelectSingleNode(string)
        L_000d: callvirt instance class [System.Xml]System.Xml.XmlNodeList [System.Xml]System.Xml.XmlNode::get_ChildNodes()
        L_0012: callvirt instance class [mscorlib]System.Collections.IEnumerator [System.Xml]System.Xml.XmlNodeList::GetEnumerator()
        L_0017: stloc.2 
        L_0018: br.s L_0055
        L_001a: ldloc.2 
        L_001b: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()
        L_0020: castclass [System.Xml]System.Xml.XmlNode
        L_0025: stloc.0 
        L_0026: nop 
        L_0027: ldloc.0 
        L_0028: ldstr "@id"
        L_002d: callvirt instance class [System.Xml]System.Xml.XmlNode [System.Xml]System.Xml.XmlNode::SelectSingleNode(string)
        L_0032: callvirt instance string [System.Xml]System.Xml.XmlNode::get_InnerText()
        L_0037: stloc.1 
        L_0038: ldloc.1 
        L_0039: call void [mscorlib]System.Console::WriteLine(string)
        L_003e: nop 
        L_003f: ldloc.1 
        L_0040: call void [mscorlib]System.Console::WriteLine(string)
        L_0045: nop 
        L_0046: ldloc.1 
        L_0047: call void [mscorlib]System.Console::WriteLine(string)
        L_004c: nop 
        L_004d: ldloc.1 
        L_004e: call void [mscorlib]System.Console::WriteLine(string)
        L_0053: nop 
        L_0054: nop 
        L_0055: ldloc.2 
        L_0056: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
        L_005b: stloc.3 
        L_005c: ldloc.3 
        L_005d: brtrue.s L_001a
        L_005f: leave.s L_007b
        L_0061: ldloc.2 
        L_0062: isinst [mscorlib]System.IDisposable
        L_0067: stloc.s CS$0$0002
        L_0069: ldloc.s CS$0$0002
        L_006b: ldnull 
        L_006c: ceq 
        L_006e: stloc.3 
        L_006f: ldloc.3 
        L_0070: brtrue.s L_007a
        L_0072: ldloc.s CS$0$0002
        L_0074: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        L_0079: nop 
        L_007a: endfinally 
        L_007b: nop 
        L_007c: ret 
        .try L_0018 to L_0061 finally handler L_0061 to L_007b
    }

  3. #3
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Quote Originally Posted by rags_to_riches View Post
    You should benchmark it if you're really interested in this, but...

    While it is possible that the JIT compiler will optimize this at run-time, based on looking at the IL it appears that the second is likely quicker (which is what I would have suspected without optimization). ...
    [/code]
    Thanks, I think so too. Specially if the XML is large, the seconds seems like a better solution.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21