Implicit/Explicit variable initialization & performance [modified]
-
Hi, Are there any difference in performance between these codes:
MyClass _myClass = new MyClass(); string _myString = ""; _myString = _myClass.GetMyString();
andMyClass _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, 2007KiT
Never wait for a chance to come, Believe in your own potential and go get it!
-
Hi, Are there any difference in performance between these codes:
MyClass _myClass = new MyClass(); string _myString = ""; _myString = _myClass.GetMyString();
andMyClass _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, 2007KiT
Never wait for a chance to come, Believe in your own potential and go get it!
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 )
-
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 )
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!
-
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!
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 )
-
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 )
I see. Thank you very much.
KiT
Never wait for a chance to come, Believe in your own potential and go get it!
-
I see. Thank you very much.
KiT
Never wait for a chance to come, Believe in your own potential and go get it!
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]