登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

[转]C++ Optimizations  

2008-05-28 22:08:14|  分类: C/C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

C++ Optimizations

These optimizations are fairly easy to apply to existing code and in some cases can result in big speedups. Remember the all-important maxim though, the fastest code is code that isn't called.

Use Initialization Lists

Always use initialization lists in constructors. For example, use

    TMyClass::TMyClass(const TData &data) : m_Data(data)
    {
    }

rather than

    TMyClass::TMyClass(const TData &data)
    {
         m_Data = data;
    }

Without initialization lists, the variable's default constructor is invoked behind-the-scenes prior to the class's constructor, then its assignment operator is invoked. With initialization lists, only the copy constructor is invoked.



Optimize For Loops

Wherever possible, count down to zero rather than up to n. For example, use

    for (i = n-1; i >= 0; --i)

rather than

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

The test is done every iteration and it's faster to test against zero than anything else. Note also that

    ++i

is faster than

    i++

when it appears in the third part of the for loop statement.



Use 'int'

Always use the int data type instead of char or short wherever possible. int is always the native type for the machine.



Make Local Functions Static

Always declare local functions as static, e.g.,

    static void foo()

This means they will not be visible to functions outside the .cpp file, and some C++ compilers can take advantage of this in their optimizations.



Optimize If Statements

Factor out jumps. For example, use

    bar();
    if (condition)
    {
         undoBar();
         foo();
    }

rather than

    if (condition)
    {
         foo();
    }
    else
    {
         bar();
    }

Use a profiler and good judgement to decide if undoing the bar() operation is faster than jumping.



Optimize Switch Statements

Put the most common cases first.



Avoid Expensive Operations

Addition is cheaper than multiplication and multiplication is cheaper than division. Factor out expensive operations wherever possible.



Initialize on Declaration

Wherever possible, initialize variables at the time they're declared. For example,

    TMyClass x = data;

is faster than

    TMyClass x;
    x = data;

Declaration then initialization invokes the object's default constructor then its assignment operator. Initializing in the declaration invokes only its copy constructor.



Pass By Reference

Always try to pass classes by reference rather than by value. For example, use

    void foo(TMyClass &x)

rather than

    void foo(TMyClass x)



Delay Variable Declarations

Leave variable declarations right until the point when they're needed. Remember that when a variable is declared its constructor is called. This is wasteful if the variable is not used in the current scope.



Use 'op='

Wherever possible, use 'op=' in favour of 'op'. For example, use

    x += value;

rather than

    x = x + value;

The first version is better than the second because it avoids creating a temporary object.



Inline Small Functions

Small, performance critical functions should be inlined using the inline keyword, e.g.,

    inline void foo()

This causes the compiler to duplicate the body of the function in the place it was called from. Inlining large functions can cause cache misses resulting in slower execution times.



Use Nameless Objects

Wherever possible, use nameless objects. For example,

    foo(TMyClass("abc"));

is faster than

    TMyClass x("abc");
    foo(x);

because, in the first case, the parameter and the object share memory.

  评论这张
 
阅读(849)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018