Click here to Skip to main content
15,917,731 members
Home / Discussions / C#
   

C#

 
GeneralRe: Binary Reader Pin
Kodanda Pani24-Apr-05 19:58
Kodanda Pani24-Apr-05 19:58 
GeneralBinary Reader Pin
Member 190895324-Apr-05 9:19
Member 190895324-Apr-05 9:19 
GeneralRe: Binary Reader Pin
Polis Pilavas24-Apr-05 10:42
Polis Pilavas24-Apr-05 10:42 
QuestionEdit TreeNode text ??? Pin
gyokusei24-Apr-05 6:47
gyokusei24-Apr-05 6:47 
AnswerRe: Edit TreeNode text ??? Pin
Polis Pilavas24-Apr-05 6:55
Polis Pilavas24-Apr-05 6:55 
AnswerRe: Edit TreeNode text ??? Pin
tommazzo24-Apr-05 10:22
tommazzo24-Apr-05 10:22 
AnswerRe: Edit TreeNode text ??? Pin
NassosReyzidis25-Apr-05 23:30
NassosReyzidis25-Apr-05 23:30 
General== or String.Equals() Pin
tommazzo24-Apr-05 6:43
tommazzo24-Apr-05 6:43 
Hi!

I'm currently trying to improve the speed of my xml library TXML, which does not rely on any XML base classes and thus parses the XML string itself to find a certain node.
Because of this I'm interested in finding the fastest way of checking the equality of two strings. On various sites I've read that String.Equals() is supposed to be faster than the == operator, which uses String.op_Equality() under the hood. So I replaced all == comparisons in a certain method with String.Equals() comparisons. The weird thing is that the new code with String.Equals() appears to run 100 ms on average slower than the old code with ==. Can anyone explain me why?

Here's the code of the above mentioned method:
private ReaderReturnValue GetNodeContent(string masterNode, int mnIndex, string subNode, int snIndex)<br />
        {<br />
            int mi = 1;        // masternode iterator<br />
            int si = 1;        // subnode iterator<br />
            int mnLength = masterNode.Length;<br />
            int snLength = subNode.Length;<br />
            int textLength = 0;    // the length of the text that is to be returned<br />
            int i = 0;            // the for iterator<br />
            int snBegin = 0;            // the beginning pos of the subnode<br />
            bool foundMn = false;<br />
            bool foundSn = false;<br />
            bool done = false;    // indicates if we have found the nodes and the text length<br />
<br />
            if(masterNode == "")        // if there is no masterNode specified set foundMn to true<br />
                foundMn = true;<br />
<br />
            for(i = 0; i < XmlText.Length; i++)<br />
            {<br />
                if(XmlText[i] == '<')            // are we currently inside a node?<br />
                {   <br />
<br />
                    if(foundMn == false)    // do we have to search for our masternode?<br />
                    {<br />
                        if(mnLength + 1 <= XmlText.Length - i - 1)            // only continue if we haven't reached the end yet - aka if there are enough chars left to read<br />
                        {<br />
                            if(XmlText.Substring(i + 1, mnLength + 1).Equals(masterNode + '>'))    // have we found our masternode?<br />
                            {<br />
                                if(mi == mnIndex)<br />
                                    foundMn = true;<br />
                                else<br />
                                    mi++;<br />
                            }<br />
                        }<br />
                    }<br />
<br />
                    if(foundMn == true && foundSn == false)    // do we have to search for our subnode?<br />
                    {<br />
                        if(snLength + 1 <= XmlText.Length - i - 1)        // only continue if we haven't reached the end yet - aka if there are enough chars left to read<br />
                        {<br />
                            if(XmlText.Substring(i + 1, snLength + 1).Equals(subNode + '>'))            // have we found our subnode?<br />
                            {<br />
                                if(si == snIndex)<br />
                                {<br />
                                    foundSn = true;<br />
                                    snBegin = i + snLength + 2;            // set the beginning of the subnode<br />
                                }<br />
                                else<br />
                                    si++;<br />
                            }<br />
                        }<br />
                    }<br />
<br />
                    if(mnLength + 1 <= XmlText.Length - i - 1)            // only continue if we haven't reached the end yet - aka if there are enough chars left to read<br />
                    {<br />
                        if(foundMn == true)                    // have we already found our masternode?<br />
                        {<br />
                            if(XmlText.Substring(i + 1, mnLength + 2).Equals('/' + masterNode + '>'))    // have we reached the end of the masternode?<br />
                                break;                            // then we'll end this loop<br />
                        }<br />
                    }<br />
<br />
                    if(foundSn == true)                    // have we already found our subnode?<br />
                    {<br />
                        if(mnLength + 1 <= XmlText.Length - i - 1)            // only continue if we haven't reached the end yet - aka if there are enough chars left to read<br />
                        {<br />
                            if(XmlText.Substring(i + 1, snLength + 2).Equals('/' + subNode + '>'))    // have we found the end of our subnode?<br />
                            {<br />
                                done = true;<br />
                                break;<br />
                            }<br />
                            else<br />
                                textLength++;<br />
                        }<br />
                    }<br />
<br />
                }            // if(XmlText[i] == '<')<br />
                else<br />
                {<br />
                    if(foundSn == true)                            // have we found all our nodes?<br />
                        textLength++;<br />
                }<br />
<br />
            }                // for...<br />
<br />
            // create the new ReaderReturnValue<br />
            ReaderReturnValue returnValue = new ReaderReturnValue();<br />
<br />
            if(done == true)                                        // have we found everything we need?<br />
            {<br />
                // fill returnValue with the node content and set foundNode to true<br />
                returnValue.text = XmlText.Substring(snBegin, textLength - snLength - 2);<br />
                returnValue.foundNode = true;<br />
            }   <br />
            else<br />
                returnValue.foundNode = false;<br />
<br />
            // return returnValue<br />
            return returnValue;<br />
        }

GeneralRe: == or String.Equals() Pin
S. Senthil Kumar24-Apr-05 19:12
S. Senthil Kumar24-Apr-05 19:12 
GeneralRe: == or String.Equals() Pin
tommazzo26-Apr-05 2:48
tommazzo26-Apr-05 2:48 
Generalwin form Pin
vuthaianh24-Apr-05 2:55
vuthaianh24-Apr-05 2:55 
GeneralRe: win form Pin
Polis Pilavas24-Apr-05 3:14
Polis Pilavas24-Apr-05 3:14 
GeneralRe: win form Pin
Nick Parker24-Apr-05 4:35
protectorNick Parker24-Apr-05 4:35 
GeneralRe: win form Pin
Polis Pilavas24-Apr-05 6:52
Polis Pilavas24-Apr-05 6:52 
GeneralExecute Stored Procedure... Pin
Illegal Operation24-Apr-05 2:53
Illegal Operation24-Apr-05 2:53 
GeneralRe: Execute Stored Procedure... Pin
Colin Angus Mackay24-Apr-05 3:42
Colin Angus Mackay24-Apr-05 3:42 
GeneralRe: Execute Stored Procedure... Pin
Illegal Operation24-Apr-05 4:05
Illegal Operation24-Apr-05 4:05 
GeneralRe: Execute Stored Procedure... Pin
Colin Angus Mackay24-Apr-05 4:11
Colin Angus Mackay24-Apr-05 4:11 
GeneralGarbage Collection - Question Pin
Tristan Rhodes24-Apr-05 0:33
Tristan Rhodes24-Apr-05 0:33 
GeneralRe: Garbage Collection - Question Pin
WillemM24-Apr-05 2:20
WillemM24-Apr-05 2:20 
GeneralIncluding a manifest file within the exe file Pin
Anonymous23-Apr-05 23:34
Anonymous23-Apr-05 23:34 
GeneralRe: Including a manifest file within the exe file Pin
DavidNohejl24-Apr-05 2:06
DavidNohejl24-Apr-05 2:06 
Generalwindows services Pin
sianatia23-Apr-05 23:18
sianatia23-Apr-05 23:18 
GeneralRe: windows services Pin
Claudio Grazioli24-Apr-05 6:14
Claudio Grazioli24-Apr-05 6:14 
GeneralList the files in another computer Pin
Johny Ng23-Apr-05 18:58
Johny Ng23-Apr-05 18:58 

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.