Is there a difference between a + = B and A = a + B?

Source: Internet
Author: User

Today I see an articleArticleIs about how to improve in. netProgramPerformance:

We recommend that you use simplified operators such as + = and-=.
For example, the original expression A = a + B
Change to a + = B
This not only reduces the number of clicksCodeBecause the variable A only appears once, it also improves the system performance during runtime.

It is true that such operations can reduce input, but do not know whether the performance can be improved and how to improve the performance (whether the code is optimized or the space is saved, with curiosity, I tried it myself. First, I wrote two test methods:

  Static     Void  Test1 ()

{
String A = " String " ;
String B = " String B " ;
A + = B;
}

Static Void Test2 ()
{
String A = " String " ;
String B = " String B " ;
A = A + B;
}

After compilation, use ildasm to check the Il values of the two methods. We can see that the Il values of the two methods are the same:

  . Maxstack    2  
. Locals Init ([ 0 ] String A,
[ 1 ] String B)
Il_0000: NOP
Il_0001: Ldstr " String "
Il_0006: Stloc.0
Il_0007: Ldstr " String B "
Il_000c: Stloc.1
Il_000d: Ldloc.0
Il_000e: Ldloc.1
Il_000f: Call String [Mscorlib] system. String: Concat ( String ,
String )
Il_0014: Stloc.0
Il_0015: RET

Since Il is the same, the code after reflector decompilation must be the same.

Here we can view the optimization level respectively:

  //  Optimization level: None  

Private Static Void Test1 () // Or Test2
{
String A;
String B;
A = " String " ;
B = " String B " ;
A = A + B;
Return ;
} // The optimization level is. Net 3.5 Private Static void test1 ()() // Or Test2
{
String A = " String " ;
String B = " String B " ;
A = A + B;
}

(The differences in the code here are the optimization of the code I wrote by the CLR platform, rather than the optimization of the ++ = discussed here. Do not confuse it)
From this point of view, the so-called performance improvement is nothing more than a syntactic sugar provided by. Net, which simplifies operations without special processing.

It is a bit of a problem, but at least it can be explained that in the course of learning, you not only need to read more articles, but also be able to think independently. It is also a good habit to verify the opinions of one person. For example, there are many suggestions on "optimization". Of course, most of them are correct (it is definitely not from the feeling, especially for the optimization aspect, it often needs to be quantified to determine), but it also requires conditions. For example, for the discussion of string and stringbuilder, it seems that no one has said that we should not use string in the future. There is also a part, in future study and practice, you need to wait until you have the ability to verify it.

Note: I have also found some articles in this regard on the Internet. It seems that there are indeed differences between the two in Java. This is not discussed in this article. But I also hope someone with good intentions can help me verify it. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.