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. Which is efficient? "" or string.Empty ?

Which is efficient? "" or string.Empty ?

Scheduled Pinned Locked Moved C#
question
9 Posts 7 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.
  • M Offline
    M Offline
    manivannan p
    wrote on last edited by
    #1

    Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.

    M J M 3 Replies Last reply
    0
    • M manivannan p

      Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Hi! Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". Regards, mav

      G N 2 Replies Last reply
      0
      • M mav northwind

        Hi! Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". Regards, mav

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        But both string.Empty and "" will be constants. The only difference is where they are declared. The constant for string.Empty already exists, so there are hardly any point of creating more constants that are identical. --- b { font-weight: normal; }

        M 1 Reply Last reply
        0
        • M mav northwind

          Hi! Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". Regards, mav

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          mav.northwind wrote: Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. If you look at the difference in the IL, string.Empty almost sounds faster.

          ldstr ""

          vs.

          ldsfld string [mscorlib]System.String::Empty

          With ldstr, it's pushing an object reference to a string literal stored in the metabase onto the stack where as ldsfld pushes the value of a static field onto the stack. mav.northwind wrote: But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". I completely agree.

          - Nick Parker Microsoft MVP - Visual C#
          My Blog | My Articles

          1 Reply Last reply
          0
          • G Guffa

            But both string.Empty and "" will be constants. The only difference is where they are declared. The constant for string.Empty already exists, so there are hardly any point of creating more constants that are identical. --- b { font-weight: normal; }

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Not entirely correct, I think. I looked it up in IL. Using the constant string.Empty compiles to ldsfld string [mscorlib]System.String::Empty whereas using "" yields ldstr "" So I'd assume the runtime would have to check at least once whether mscorlib is loaded already (which is, of course) to be able to access a constant defined in a type from this assembly. Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, but, as I wrote, I don't think you'll be able to notice it. Regards, mav

            L 1 Reply Last reply
            0
            • M manivannan p

              Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              On a related note, if you're checking to see whether a string is empty, it is more efficient to use if(str.Length == 0) instead of if(str == "") or if(str == string.Empty)

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Cops & Robbers Judah Himango

              1 Reply Last reply
              0
              • M mav northwind

                Not entirely correct, I think. I looked it up in IL. Using the constant string.Empty compiles to ldsfld string [mscorlib]System.String::Empty whereas using "" yields ldstr "" So I'd assume the runtime would have to check at least once whether mscorlib is loaded already (which is, of course) to be able to access a constant defined in a type from this assembly. Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, but, as I wrote, I don't think you'll be able to notice it. Regards, mav

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                mav.northwind wrote: Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, Here is the optimized JIT code. Same thing... except with "" you will be adding more strings to the string table of the assembly.

                16: string a = "";
                0000000f mov eax,dword ptr ds:[01AA1010h]
                00000015 mov esi,eax
                17: string b = string.Empty;
                00000017 mov eax,dword ptr ds:[01AA2014h]
                0000001c mov edi,eax

                xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                G 1 Reply Last reply
                0
                • L leppie

                  mav.northwind wrote: Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, Here is the optimized JIT code. Same thing... except with "" you will be adding more strings to the string table of the assembly.

                  16: string a = "";
                  0000000f mov eax,dword ptr ds:[01AA1010h]
                  00000015 mov esi,eax
                  17: string b = string.Empty;
                  00000017 mov eax,dword ptr ds:[01AA2014h]
                  0000001c mov edi,eax

                  xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  I expected it to be very little difference in the compiled code, but I didn't expect it to be as good as no difference at all. :) Anyway, regardless of how it is currently implemented, the code that best resembles your intention will often be the most efficient. The compiler will be able to optimize the code better if the information you give it is better. In some future implementation or on a different system, using string.Empty might even be the most efficient way. --- b { font-weight: normal; }

                  1 Reply Last reply
                  0
                  • M manivannan p

                    Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.

                    M Offline
                    M Offline
                    Mohamad Al Husseiny
                    wrote on last edited by
                    #9

                    May be this helpString.Empty vs "" [^] MCAD

                    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