Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Crafting Strings in C#-2 : Page 2


advertisement
Testing the Summing
Use this small application to test the summing:

const string a = "ABC"; Int32 b = 1; Int32 c = 2; string s = a + b + c; Console.WriteLine(s);

When you compile it and look at the IL code, you'll find that the C# compiler has boxed b and c. Here's an extract from the IL code:

. . . .locals init ([0] int32 b, [1] int32 c, [2] string s) IL_0000: ldc.i4.1 IL_0001: stloc.0 IL_0002: ldc.i4.2 IL_0003: stloc.1 IL_0004: ldstr "ABC" IL_0009: ldloc.0 IL_000a: box [mscorlib]System.Int32 IL_000f: ldloc.1 IL_0010: box [mscorlib]System.Int32 . . .

Next, concatenate the boxed values (reference type):


IL_0015: call string [mscorlib]System.String::Concat(object, object, object)

Lessons Learned
What are the lessons learned here?

  1. State What You Want: In the above example, it's clear that the summing is performed as a concatenation of the string representations of the values of ABC, 1, and 58. This is why the value logged in at 158 instead of 59. Adding parentheses solves the problem:

    string s = a + (b + c);

  2. Read the Language Specification: Looking at section 14.7.4 in the Standard ECMA-334 - C# Language Specification, you'll find this statement:

    "...any non-string operand is converted to its string representation by invoking the virtual ToString method inherited from type object."

    This means that the ToString() method is called on each object by the Concat overload in the above IL code.

  3. Use Methods of Int32 Structure: So if ToString() is called on object, why not call it directly on (b+c)? Eliminate the expensive boxing used by the C# compiler and the code will be executed faster. The ToString() method of the Int32 structure converts the numeric value of the instance to its equivalent string representation. This way, the C# compiler uses the Concat(string, string) overload method. Here's what the code looks like:

    string s = a + (b + c).ToString();

    This code runs about 30 percent faster. Thus, the shortest code is not the fastest code!

Author's Note: While in some cases, the Append() method of the StringBuilder class performs better than the Concat method, a discussion of these cases is outside the scope of this material.



Boris Eligulashvili is a Senior Engineer at Viewray, Inc. Prior to joining Viewray, he worked his way through different software development positions for Allen-Bradley, Saint-Gobain Crystals and Detectors, Philips Medical Systems, and Hitachi Medical Systems America, Inc. He has worked with the latest technologies as they have evolved throughout the years—from raw machine code to .NET. His current focus is in developing software solutions for a novel MRI-guided radiotherapy device.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap