If the knife is not worn, it will rust. If you do not program for a long time, you will also forget many key things. One of them is to forget the problems that should be paid attention to in daily programming and write them slowly.CodeIt's so bad that you can't afford to read it .....
Programming is a meticulous work, with many traps. One of them is that we are easy to write deteriorating code. Many people cannot figure out how to write their own code and no one else can execute it efficiently, so here I will write some of my own knowledge in this area, to prevent myself from forgetting, and to share with you, especially for beginners.
A considerable number of people think that the content I wrote should be code optimization level, but I do not think so. I think Optimization in programming should not be too early, but it should be done at any time to avoid deteriorating code. Some of the content I am talking about here is a conventional talk, so I will explore it in depth and try to find new wine in the old bottle. Some are little-known, so I will elaborate on it in detail and try to make it easy to understand.
To avoid code degradation, this topic is not limited to C and C ++, but also includes python, Lua, and other familiar scripting languages, or even boost, openMP and other libraries may also be used to optimize SQL statements. I hope you can join us more ~
This seriesArticleWelcome to reprint, print, distribution, etc., but cannot be used for commercial purposes, at any time must keep the full text complete, and the statement reproduced narcissistic butterfly blog (http://blog.csdn.net/lanphaday), thank you.
Avoid degradation code (1)
No inferior code
,
(1)
Clause 1
, Use the copy constructor whenever possible to initialize instance variables instead of using the value assignment operator.
Example: # Include <ctime> # Include <iostream> # Include <string> Void test_inferior_code (int cnt, const char * Str) { For (INT I = 0; I <CNT; ++ I) { STD: String S; S = STR; S [0] = 'H '; } } Void test_excellent_code (int cnt, const char * Str) { For (INT I = 0; I <CNT; ++ I) { STD: String S (STR ); S [0] = 'H '; } } Int main () { Static const int CNT = 1000000; Char STR [] = "hello "; Clock_t BGN = clock (); Test_inferior_code (CNT, STR ); Clock_t end = clock (); STD: cout <"test_inferior_code used" <End-BGN <"MS/N "; BGN = clock (); Test_excellent_code (CNT, STR ); End = clock (); STD: cout <"test_excellent_code used" <End-BGN <"MS/N "; Return 0; }
Analysis: On my machine (for the configuration and compilation and execution environment, see the text below), the output of the above example is as follows: $./No_inferior_code1.exe Test_inferior_code used 4336 Ms Test_excellent_code used 3485 Ms We can see that the version execution time using the value assignment operator is 1.2 times that of the copy constructor method. This value is not surprising, but it cannot be ignored. Because the example only uses STD: String. If it is a STD: map with multiple elements, what if it is a user-defined class? Therefore, it is very important not to take good and small, not to take evil as small as possible, and to develop the habit of using copy constructors as much as possible. The above example is unlikely to appear in our code. In reality, this deterioration is the most likely case: Class { Public: A (const STD: string & Str) {_ STR = STR ;} PRIVATE: STD: String _ STR; }; Such code is easy to see. It is actually a type of deteriorating code. It is better to write like this: Public: A (const STD: string & Str): _ STR (STR ){} Use the constructor list to initialize member variables.
Extension: All the way to talking about using STL and other people's libraries, if you want to write a class for others, what should you pay attention? Yes, we should provide interfaces or methods that allow others to write excellent code. To put it simply, if the class we provide reloads the value assignment operator, a copy constructor should be provided accordingly. In turn, if a copy constructor is provided, you also need to overload the value assignment operator. If you do not intend to do this, you must declare the interface you do not provide as private or protected to avoid misunderstanding caused by the compiler's Automatic Generation. Class { Public: A (const A & A): _ STR (A. _ Str ){} A & operator = (const A & A) {_ STR = A. STR; return * This ;} STD: String STR; }; The above code is an example that can be computed ~