== or String.Equals()
-
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) { int mi = 1; // masternode iterator int si = 1; // subnode iterator int mnLength = masterNode.Length; int snLength = subNode.Length; int textLength = 0; // the length of the text that is to be returned int i = 0; // the for iterator int snBegin = 0; // the beginning pos of the subnode bool foundMn = false; bool foundSn = false; bool done = false; // indicates if we have found the nodes and the text length if(masterNode == "") // if there is no masterNode specified set foundMn to true foundMn = true; for(i = 0; i < XmlText.Length; i++) { if(XmlText[i] == '<') // are we currently inside a node? { if(foundMn == false) // do we have to search for our masternode? { 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 { if(XmlText.Substring(i + 1, mnLength + 1).Equals(masterNode + '>')) // have we found our masternode? { if(mi == mnIndex) foundMn = true; else mi++; } } } if(foundMn == true
-
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) { int mi = 1; // masternode iterator int si = 1; // subnode iterator int mnLength = masterNode.Length; int snLength = subNode.Length; int textLength = 0; // the length of the text that is to be returned int i = 0; // the for iterator int snBegin = 0; // the beginning pos of the subnode bool foundMn = false; bool foundSn = false; bool done = false; // indicates if we have found the nodes and the text length if(masterNode == "") // if there is no masterNode specified set foundMn to true foundMn = true; for(i = 0; i < XmlText.Length; i++) { if(XmlText[i] == '<') // are we currently inside a node? { if(foundMn == false) // do we have to search for our masternode? { 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 { if(XmlText.Substring(i + 1, mnLength + 1).Equals(masterNode + '>')) // have we found our masternode? { if(mi == mnIndex) foundMn = true; else mi++; } } } if(foundMn == true
I looked at the implementation of op_Equality and Equals. It looks like op_Equality ends up calling Equals in its implementation, after doing some null checking. I don't know why calling op_Equality is faster. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
I looked at the implementation of op_Equality and Equals. It looks like op_Equality ends up calling Equals in its implementation, after doing some null checking. I don't know why calling op_Equality is faster. Regards Senthil _____________________________ My Blog | My Articles | WinMacro