C++性能

C++性能调优:

C++性能调优方面的几个基本原理:

  1. I/O 的开销是昂贵的
  2. 函数调用的开销是一个因素,因此我们应该内联短小的并且频繁调用的函数
  3. 复制对象的开销是昂贵的,最好是按照引用传递,而不是按值传递

但是上述三条准则没有涵盖所有的准则,C++ 性能提高的主要问题是对不必要的对象的创建以及后面的清除,这些不必要的对象预计要使用但是实际并没有使用。

构造函数和析构函数:

继承:在构造函数和析构函数由于设计等方面的需求,创建对象和清除对象时长会导致性能的损失,在继承层次中,创建对象将引起其父类的创建,清除对象也是如此,其次,对象相关联的的开销直接与派生链的长度和复杂性有关。创建对象的数量和派生类的复杂性成比例。

合成:在某些情况下我们可以为指针分配一个无效指,使得创建和清除的开销达到最小。

不高效的方法:

Trace::Trace(const char* name): theFunctionName(new string(name))

高效的方法:

Trace::Trace(const char* name): theFunctionName(0){

    if(traceIsActive){

       theFunctionName = new string(name);

    }

}

缓式构造:C++自然而然的预先定义所有的对象的习惯是一种浪费。这和C语言不通,C语言在定义变量的时候没有运行时候的开销。

 

 

C++内存模型:

  1. 正常

(2)正常继承

(3)继承有重写

(4)多继承

https://i-blog.csdnimg.cn/blog_migrate/3306339cce843fbc5dd7e6607b2d52f0.pnghttps://i-blog.csdnimg.cn/blog_migrate/6a781640dd9cd5b57750e587571edbba.png

(5)单继承 虚继承

https://i-blog.csdnimg.cn/blog_migrate/ca658f89015f7d26d4498c63b2a76bd3.png

(6)多继承虚继承

https://i-blog.csdnimg.cn/blog_migrate/2aee6b2f31119ed0c21b3bb38480dc6a.jpeg

https://i-blog.csdnimg.cn/blog_migrate/ca658f89015f7d26d4498c63b2a76bd3.png

内联函数的使用:小、微常用的函数使用内联函数。一般递归不要使用内联函数。

系统设计以及缓存等优化:设计经验十分重要。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++性能优化 指南(强列推荐) chm版 Part I: Everything But the Code Chapter 1. Optimizing: What Is It All About? Performance Footprint Summary Chapter 2. Creating a New System System Requirements System Design Issues The Development Process Data Processing Methods Summary Chapter 3. Modifying an Existing System Identifying What to Modify Beginning Your Optimization Analyzing Target Areas Performing the Optimizations Summary Part II: Getting Our Hands Dirty Chapter 4. Tools and Languages Tools You Cannot Do Without Optimizing with Help from the Compiler The Language for the Job Summary Chapter 5. Measuring Time and Complexity The Marriage of Theory and Practice System Influences Summary Chapter 6. The Standard C/C++ Variables Variable Base Types Grouping Base Types Summary Chapter 7. Basic Programming Statements Selectors Loops Summary Chapter 8. Functions Invoking Functions Passing Data to Functions Early Returns Functions as Class Methods Summary Chapter 9. Efficient Memory Management Memory Fragmentation Memory Management Resizable Data Structures Summary Chapter 10. Blocks of Data Comparing Blocks of Data The Theory of Sorting Data Sorting Techniques Summary Chapter 11. Storage Structures Arrays Linked Lists Hash Tables Binary Trees Red/Black Trees Summary Chapter 12. Optimizing IO Efficient Screen Output Efficient Binary File IO Efficient Text File IO Summary Chapter 13. Optimizing Your Code Further Arithmetic Operations Operating System–Based Optimizations Summary Part III: Tips and Pitfalls Chapter 14. Tips Tricks Preparing for the Future Chapter 15. Pitfalls Algorithmic Pitfalls Typos that Compile Other Pitfalls
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值