Recommendation 1: Use strings correctly
String
String str1 = "str1" + 9;string str2 = "str2" + 9.ToString ();
The first line of code produces one boxing, and one string concat
The second line of code uses ToString (), which internally uses the Number.formatint32
Its prototype is
While NumberFormatInt32 is an unmanaged method that runs much more efficiently than normal C # managed code, the second line of code is more efficient than the first line
As for what is managed code unmanaged code, I quote a phrase from another blog:
Managed code and unmanaged code As we all know, the high-level language we use for normal programming cannot be recognized by computers. The high-level language needs to be translated into machine language before it can be understood and run by the machine. in standard C + +, the compilation process is like this:
The source code first passes through the preprocessor, the head file and the macro parse, then passes through the compiler, generates the assembly code, then, through the assembly, generates the machine instruction, finally connects all the files together. The advantage of this method is that the machine code is generated directly, can be directly recognized and run by the computer, without any intermediate operating environment, but the disadvantage is that because different platforms can recognize different machine codes, the program's cross-platform capability is poor. In the Java language, the source code is not translated directly into the machine code, but rather compiled into an intermediate code (bytecode bytecode). Therefore, running a Java program requires an additional JRE (Java Runtime Enviromental) runtime environment, where the JVM (Java Virtual Mechinal,java virtual machine) exists in the JRE, and when the program runs, the intermediate code is further interpreted as machine code and run on the machine. The advantage of using intermediate code is that the program has a better cross-platform, one-time compilation, and can be run on different devices. Managed/unmanaged is a unique concept in Microsoft's. NET Framework, where unmanaged code is also called local (native) code. Similar to the mechanism in Java, the source code is first compiled into intermediate code (Msil,microsoft intermediate Language), and then the CLR in. NET compiles the intermediate code into machine code. The difference between C # and Java is that Java is compiled and interpreted, and C # is compiled two times. In addition to having cross-platform advantages, the managed approach has a certain impact on the performance of the program. However, the performance of the program is not covered in this article, not here. Also, in. NET, C + + can carry out Managed extensions, so that C + + code also relies on the. NET and CLR to run, gaining the benefits of managed code. |
The simple understanding is that managed code adds a middle tier that makes it cross-platform, but less efficient, and not managed code.
So we write code adhering to one principle: minimizing packing and unpacking
StringBuilder
StringBuilder efficiency comes from the pre-unmanaged way of allocating memory, if there is no predefined length, the default length is 16, not enough time to reallocate memory, and then add a multiple of 16, so if you know in advance the maximum length of the string, it is best to pre-defined, This makes it less prone to allocating memory, resulting in reduced efficiency
2. Using the default transformation method
The general content of this recommendation is to try to use the method of the system's conversion type
such as Int. Parse ToString () System.Convert, etc.
3. Distinguish between the strong turn type and as is
There are two cases of conversion between two types
1. They are the parent-child relationship: Childtype = (childtype) ParentType
2. There is no inheritance, or inheritance of the same parent class, you need to override the strong-turn method
Class firsttype{Public string Name {get; set;}} Class secondtype{Public string Name {get; set;} public static explicit operator Secondtype (Firsttype firsttype) { Secondtype secondtype = new Secondtype () {Name = Firsttype.name}; return secondtype;} } Firsttype Firsttype = new Firsttype () {Name = "Zhang"}; Secondtype Secondtype = (secondtype) firsttype;
If it is an inherited relationship, for efficiency it is recommended to use Childtype = ParentType as Childtype
This is more of the above, as far as possible, using the conversion of the system method instead of casting
When I wrote unity, there was a need to right-click on the rig and use it, equipped with equipment,weapon, we need to determine whether it is equipment or weapon type, they are inherited item types, there are two methods available:
Weapon weapon = Item as weapon;if (weapon! = null) { //todo use weapon}if (item is weapon) { Weapon weapon = Item as weapon; //todo using Weapons}
The first method only determines one type at a time, while the second method determines the two-time type
The book says as cannot judge the primitive type, but after my test found that the book is wrong, as only can not judge the value type, here you can test their own
Related articles:
C # Learning Record: Writing high-quality code improvement recommendations 4-8
C # Learning Record: Writing high-quality code improvement recommendations 9-15