Optimize source code C++
optimize C++
Male avatar

Quan Rock viết ngày 07/01/2016

Today, I really keen on writing a post about my experience during 3 years working with C++. These knowledge will help you improve code skills and performance of your project. Let 's go.

Because C++ reach to C++11 and C++14 at the moment, so , this post will update the newest skills. This guide looks like Effective C++.

Some knowledge is for senior C++ developer, but , almost is easy to understand for all people.

1/ Switch case is better !!

Suppose you have codes below :

const int value = 15;
switch (value)
           case 10:
                   std::cout << value + 1;
           case 11:
                   std::cout << value + 2;
           case 15:
                   std::cout << value;

The assembly generated by compiler by using switch-case look like :

mov eax,dword ptr [value] 
     mov dword ptr [ebp-0D0h],eax 
     cmp dword ptr [ebp-0D0h],0Ah 
     je wmain+4Bh (case10-adrr) 
     cmp dword ptr [ebp-0D0h],0Bh 
     je wmain+69h (case11-adrr) 
     cmp dword ptr [ebp-0D0h],0Fh 
     je wmain+87h (case15-adrr) 
     jmp wmain+0A0h (default-adrr) ```

You could realize that program will sequentially compare to value of "case" and if something wrong, it will go next step, without jump to anywhere because of opcode "je".

And with if-else :

int value = 15;
  mov         dword ptr [value],0Fh  
    if(value == 10)
  cmp         dword ptr [value],0Ah  
  jne         wmain+49h (value == 11_adrr)  
        std::cout << value + 1;
    else if(value == 11)
  cmp         dword ptr [value],0Bh  
  jne         wmain+6Dh (value == 15_adrr)  
        std::cout << value + 1;
  jmp         wmain+89h (outofifelse_adrr)  
        std::cout << value + 1;

Clearly, if-else have to jump more places than switch-case. And, totally , switch-case win 1-0.

2/ add value better than statement = inline function adder

// using inline if possible

// set calculation inside loop outside

// using template if possible

// all params is const ref

// optimize condition

// do not put object to constructor

// should not init so much , not run method(especially virtual method) in constructor

// override carefully ( default param sometime conflict, pay attention to Base method and current method)

// Assign by put data to Class in declare -> class A : Age(age) , Name(name)

// if need => overload operator =;

// put data to function and set value to them, don't create new object and return inside method

// avoid temporary object

// function with const => int getNumber() const;

// pre-processor is good performance !!

// about virtual table of implement

// performance among containers

// C++11 and C++ 14

Bình luận

{{ comment.user.name }}
Bỏ hay Hay
Male avatar
{{ comment_error }}

Hiển thị thử

Chỉnh sửa

Male avatar

Quan Rock

1 bài viết.
0 người follow
{{userFollowed ? 'Following' : 'Follow'}}


{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}

Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
1 bài viết.
0 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!