Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. == or String.Equals()

== or String.Equals()

Scheduled Pinned Locked Moved C#
comxmlperformancequestioncode-review
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tommazzo
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • T tommazzo

      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

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • S S Senthil Kumar

        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

        T Offline
        T Offline
        tommazzo
        wrote on last edited by
        #3

        Due to some misconfiguration in my Profiler I originally thought op_Equality() to be faster, which in fact isn't the true case. Apparently the null checking that is performed in op_Equality() makes it a little slower than Equals(). Thanks for your help!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups