惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
AI
AI
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes
V
Vulnerabilities – Threatpost
S
Securelist
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Cisco Talos Blog
Cisco Talos Blog
Cloudbric
Cloudbric
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
D
Docker
博客园 - 司徒正美
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
O
OpenAI News
人人都是产品经理
人人都是产品经理
腾讯CDC
博客园 - 三生石上(FineUI控件)
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Scott Helme
Scott Helme
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
N
News and Events Feed by Topic
J
Java Code Geeks
D
DataBreaches.Net
爱范儿
爱范儿
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
H
Heimdal Security Blog
F
Full Disclosure
L
LINUX DO - 最新话题
W
WeLiveSecurity
Blog — PlanetScale
Blog — PlanetScale
V
V2EX
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42

博客园 - SoRoMan

Scene Management - Scene Graph 思考:矩阵的级联顺序与坐标系的关系 MFC的一些宏的整理 (DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE) 关于STL Improve Performance of C++ Codes (2) -- 如何消除临时对象? 关于万向节死锁(Gimbal Lock)(续) 思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用问题:左乘/右乘,行优先/列优先,... 读书笔记:Writing Solid Code (4) 读书笔记:Writing Solid Code (3) 读书笔记:Writing Solid Code (2) 读书笔记:Writing Solid Code A 3D Real Time Fluid Solver Demo (三维实时流体解算器) 笔记:Delaunay三角剖分(Delaunay Triangulation)相关知识 学习经验:逐步建立学习资源列表-计算机图形学 SL专题6:谈谈Second Life中的游戏制作(Game Dev in SL) SL专题4:Second Life畅销商品 SL专题3:Second Life市场分析 SL专题2:加入并熟悉Second Life世界 SL专题1:Second Life(第二生命)--迟早火爆世界的游戏与Web的集大成者
Improve Performance of C++ Codes (1) -- 使用初始化列表还是赋值语句?
SoRoMan · 2008-04-05 · via 博客园 - SoRoMan

就提高程序的性能/效率而言,上层的平台/架构/算法/数据结构当然重要,然而也不能忽视代码本身的性能优化,即为了让编译器将你写的高级语言的代码翻译成尽量高效的机器代码,这方面也是我很感兴趣的领域,下面就记录一些improve performance of c++ codes的方方面面,希望从点滴做起,写出相对高效的c++codes。

条款1:
--------------------------------
Q:构造函数中使用初始化列表(Initialization list)还是赋值(Aassignment)来初始化成员?
A:尽量使用初始化列表。
--------------------------------

测试:
--------------------------------
(测试主要集中于在x86平台上的MS编译器,且是在开启编译器的最大优化功能的情况下,一般对应Release模式)
下面看个c++ test program:

//TestClass.h
class A
{
public:
    A();
    int i;
};

//TestClass.cpp
#include "TestClass.h"

A::A()
{
    i = 0;
}

//LGVector.h
#include "TestClass.h"

class LGVector
{
public:
    // constructor
    LGVector(){};
    LGVector(A a);

private:
    int i;
    A a;    
};

//LGVector.cpp
#include "TestClass.h"

// Initializaton list
LGVector::LGVector(A a) : a(a)
{
}

// Assignment
//LGVector::LGVector(A a) : a(a)
//{
//    a = a;
//}

//main.cpp
#include "LGVector.h"

int main(int argc, char* argv[])
{    
    A a1;
    a1.i = 10;

    LGVector v1(a1);

    return 0;
}

下面是LGVector构造函数的x86 Assembly codes:

初时化列表:(x86, VC++ 6.0, Release, Optimization: Maximize)

_a$ = 8
??0LGVector@@QAE@VA@@@Z PROC NEAR            ; LGVector::LGVector, COMDAT

; 14   : {    

    mov    eax, ecx
    mov    ecx, DWORD PTR _a$[esp-4]  // 不会调用A的构造函数
    mov    DWORD PTR [eax+4], ecx

; 15   : }

    ret    4

赋值操作:(x86, VC++ 6.0, Release, Optimization: Maximize)

_a$ = 8
??0LGVector@@QAE@VA@@@Z PROC NEAR            ; LGVector::LGVector, COMDAT

; 7    : {

    push    esi
    mov    esi, ecx
    push    edi
    lea    edi, DWORD PTR [esi+4]
    mov    ecx, edi
    call    ??0A@@QAE@XZ                ; A::A // 会调用A的构造函数,注意:并没有分配额外的内存来创

建额外的对象,只是将当前对象的的子对象a的指针(edi)传递到A:A()中,做一些初时化。performance的下降只在于A:A()的调用。

; 8    :     this->a = a;

    mov    eax, DWORD PTR _a$[esp+4]
    mov    DWORD PTR [edi], eax

; 9    : }

    mov    eax, esi
    pop    edi
    pop    esi
    ret    4

--------------------------------

小结:
--------------------------------
VC++ 6.0下:
在构造函数中,推荐使用初时化列表来初始化成员,可以避免成员的构造函数的调用,提高performance,但只针对
non built-in/instinctive types,因为built-in/instinctive types没有构造函数。

在VC++2005:
貌似两者performance一样,赋值操作来初始化成员也不会调用成员的构造函数。

--------------------------------