c / c ++ code optimization and common errors

table of Contents

1. Code Optimization

for loop

Bit computing

input Output

Playing table method (cheat points only)

Array merge

 Reduction function parameters

Recursive (search) pruning

 Array row access

Block

2. often a mistake

For equality

Spelling mistakes


1. Code Optimization

  • for loop

     Non-optimized code:

for(i=0;i<sqrt(n);i++)

      for (single expression ; conditional expression ; the end of the loop ), written in the middle of the end condition, the end of a cycle of comparison and determination will be performed, if a greater amount of cycles, sqrt function will run multiple times.

     Optimization Method:

       With variable holds, not for circulation inside the function. while loop and the like.

     Optimized code:

int bound = sqrt(n);
for(int i=0;i<bound;i++)
  • Bit computing

     Bitwise determined parity integer variable, modulo terms, variables, etc. can accelerate the exchange. Alone wrote an article, welcome to share more magical bit operation.  

     Magical c language-bit computing - Optimization program

  • input Output

     c language input and output scanf and printf is faster than c ++ cin and cout of the , so, when there are a large number of inputs and outputs, select the c language.

     c / c ++ input and output summary

  • Playing table method (cheat points only)

        Table method is to play the title need answers calculated in advance, stored in the array. This method only need to program hangs, and then do another problem. After the calculated results, copied to the array inside. The final answer procedures take time complexity is O (1), the space complexity is O (n-);
                                                                                                                                                                             - Wards shuttle de-yl

  • Array merge

    The relevant data together, although in different arrays, the subject does not perform the sorting operation result does not correspond, but the data in the Cache is not necessarily two arrays together. The structure can be used, for example, students.

before fixing:

int id[maxn];
string name[maxn];

Modified:

typedef struct student
{
   int id;
   string name;
}student;

student S[maxn];
  •  Reduction function parameters

         When using a recursive writing, array or the like may be set as a global variable, reduced function of the parameters can be reduced some time.

  • Recursive (search) pruning

       Reduce unnecessary search, there will be a function of the stack when the recursive call, a waste of time.

      Recursive - simple knapsack problem (recursive tree pruning, containing all the code)

  •  Array row access

        Students learned computer system architecture should know, for two-dimensional array, row access can not reduce the Cache hit rate, can be partially optimized. Most teachers are taught row access priority.

  • Block

       When the matrix operation, the first sub-block operation. Bloggers currently matrix operations involving very little, even more when encountered.

2. often a mistake

  • For equality

       When we do the math, always use the "=" means equal, and in c / c ++ is assigned is equal "==." Often when an error condition is determined, the equivalent written in the wrong assignments.

    error code:

if(i=1)cout<<“只有1个”<<endl;
else cout<<“没有了”<<endl;

     Prevents way:

      When want to judge equal, the value written in the left side. For example, 1 == i , so there is no problem, but if you're not careful wrong, written 1 = i , the compiler will complain, because you assign a variable constant.

     Correcting Code:

if(1==i)cout<<“只有1个”<<endl;
else cout<<“没有了”<<endl;
  • Spelling mistakes

Sometimes the error, check the code, probably because int mian or flase such as misspelling, I feel very funny, but also a waste of time.

        Prevents way:

        The use of macros, # define correct errors, such as #define flase false.

Unfinished, continued ...

More data structures and algorithms: a data structure (Yan Wei Min Edition) and algorithm (including all code)

Have questions, please comment below, please indicate the source, along with the original link, thank you! If infringement, please contact.

发布了163 篇原创文章 · 获赞 471 · 访问量 26万+

Guess you like

Origin blog.csdn.net/lady_killer9/article/details/88068702
Recommended