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. Implicit/Explicit variable initialization & performance [modified]

Implicit/Explicit variable initialization & performance [modified]

Scheduled Pinned Locked Moved C#
performance
6 Posts 3 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.
  • A Offline
    A Offline
    AesopTurtle
    wrote on last edited by
    #1

    Hi, Are there any difference in performance between these codes: MyClass _myClass = new MyClass(); string _myString = ""; _myString = _myClass.GetMyString(); and MyClass _myClass = new MyClass(); string _myString = _myClass.GetMyString(); In the first case, the string variable has been explicitly initialized. While in the second case, it has been implicitly initialized. Well, I understand that the difference in performance cannot be noticed. But I'm just curious to learn about it. Thank you very much. -- modified at 22:15 Wednesday 20th June, 2007

    KiT Never wait for a chance to come, Believe in your own potential and go get it!

    C 1 Reply Last reply
    0
    • A AesopTurtle

      Hi, Are there any difference in performance between these codes: MyClass _myClass = new MyClass(); string _myString = ""; _myString = _myClass.GetMyString(); and MyClass _myClass = new MyClass(); string _myString = _myClass.GetMyString(); In the first case, the string variable has been explicitly initialized. While in the second case, it has been implicitly initialized. Well, I understand that the difference in performance cannot be noticed. But I'm just curious to learn about it. Thank you very much. -- modified at 22:15 Wednesday 20th June, 2007

      KiT Never wait for a chance to come, Believe in your own potential and go get it!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If you replace "" with string.Empty, then I think performance will be equal.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      A 1 Reply Last reply
      0
      • C Christian Graus

        If you replace "" with string.Empty, then I think performance will be equal.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        A Offline
        A Offline
        AesopTurtle
        wrote on last edited by
        #3

        So you mean in this scenario, the second case (implicitly initialization) yields better performance?

        KiT Never wait for a chance to come, Believe in your own potential and go get it!

        C 1 Reply Last reply
        0
        • A AesopTurtle

          So you mean in this scenario, the second case (implicitly initialization) yields better performance?

          KiT Never wait for a chance to come, Believe in your own potential and go get it!

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          yes, because the first creates an empty string, which is then discarded. string.Empty maps to an empty string that already exists, so doesn't cost anything.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          A 1 Reply Last reply
          0
          • C Christian Graus

            yes, because the first creates an empty string, which is then discarded. string.Empty maps to an empty string that already exists, so doesn't cost anything.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            A Offline
            A Offline
            AesopTurtle
            wrote on last edited by
            #5

            I see. Thank you very much.

            KiT Never wait for a chance to come, Believe in your own potential and go get it!

            L 1 Reply Last reply
            0
            • A AesopTurtle

              I see. Thank you very much.

              KiT Never wait for a chance to come, Believe in your own potential and go get it!

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              KiTsuNeKo wrote:

              Never wait for a chance to come, Believe in your own potential and go get it!

              You can use Reflector[^] to investigate MSIL code... :)

              Luc Pattyn [My Articles] [Forum Guidelines]

              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