编程小贴士

给你的编程提供小点子


减少C++代码编译时间的方法

c++的代码包含头文件和实现文件两部分,头文件一般是提供给别人(也叫客户)使用的,但是一旦头文件发生改变,不管多小的变化,所有引用他的文件 就必须重新编译,编译就要花时间,假如你做的工程比较大(比如二次封装chrome这类的开发),重新编译一次的时间就会浪费上班的大部分时间,这样干了 一天挺累的,但是你的老板说你没有产出,结果你被fired,是不是很怨啊,如果你早点看到这段文章,你就会比你的同事开发效率高那么一些,那样被 fired就不会是你了,你说这篇文章是不是价值千金!开个玩笑:)

言归正传,怎样介绍编译时间呢,我知道的就3个办法:

1.删除不必要的#include,替代办法使用前向声明(forwarddeclared)

2.删除不必要的一大堆私有成员变量,转而使用”impl”方法

3.删除不必要的类之间的继承

为了讲清楚这3点,还是举个实例比较好,这个实例我会一步一步的改进(因为我也是一点一点摸索出来了,如果哪里说错了,你就放心的喷吧,我会和你在争论到底的,呵呵)

现在先假设你找到一个新工作,接手以前某个程序员写的类,如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

// old.h: 这就是你接收的类

//

#include <iostream>

#include <ostream>

#include <list>

// 5 个 分别是file , db, cx, deduce or error , 水平有限没有模板类

// 只用 file and cx 有虚函数.

#include "file.h" // class file

#include "db.h" // class db

#include "cx.h" // class cx

#include "deduce.h" // class deduce

#include "error.h" // class error

classold :publicfile,privatedb {

public:

old(constcx& );

db get_db(int,char* );

cx get_cx(int, cx );

cx& fun1( db );

error fun2( error );

virtualstd::ostream& print( std::ostream& )const;

private:

std::list<cx> cx_list_;

deduce deduce_d_;

};

inlinestd::ostream& operator<<( std::ostream& os,constold& old_val )

{returnold_val.print(os); }

 

这个类看完了,如果你已经看出了问题出在哪里,接下来的不用看了,你是高手,这些基本知识对你来说太小儿科,要是像面试时被问住了愣了一下,请接着看吧

先看怎么使用第一条:删除不必要的#include

这个类引用5个头文件,那意味着那5个头文件所引用的头文件也都被引用了进来,实际上,不需要引用5个,只要引用2个就完全可以了

1.删除不必要的#include,替代办法使用前向声明(forwarddeclared)

 

1.1删除头文件iostream,我刚开始学习c++时照着《c++primer》抄,只要看见关于输入,输出就把iostream头文件加上,几年过去了,现在我知道不是这样的,这里只是定义输出函数,只要引用ostream就够了

 

1.2.ostream头文件也不要,替换为iosfwd,为什么,原因就是,参数和返回类型只要前向声明就可以编译通过,在iosfwd文件里678行(我的环境是vs2013,不同的编译环境具体位置可能会不相同,但是都有这句声明)有这么一句

typedefbasic_ostream<char,char_traits<char>>ostream;

inlinestd::ostream&operator<<(std::ostream&os,constold&old_val)

{returnold_val.print(os);}

除此之外,要是你说这个函数要操作ostream对象,那还是需要#include<ostream>,你只说对了一半,的确,这个函数要操作ostream对象,但是请看他的函数实现,

里面没有定义一个类似std::ostreamos,这样的语句,话说回来,但凡出现这样的定义语句,就必须#include相应的头文件了,因为这是请求编译器分配空间,而如果只前向声明classXXX;编译器怎么知道分配多大的空间给这个对象!

看到这里,old.h头文件可以更新如下了:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

// old.h: 这就是你接收的类

//

#include <iosfwd> //新替换的头文件

#include <list>

// 5 个 分别是file , db, cx, deduce or error , 水平有限没有模板类

// 只用 file and cx 有虚函数.

#include "file.h" // class file , 作为基类不能删除,删除了编译器就不知道实例化old 对象时分配多大的空间了

#include "db.h" // class db, 作为基类不能删除,同上

#include "cx.h" // class cx

#include "deduce.h" // class deduce

// error 只被用做参数和返回值类型, 用前向声明替换#include "error.h"

classerror;

classold :publicfile,privatedb {

public:

old(constcx& );

db get_db(int,char* );

cx get_cx(int, cx );

cx& fun1( db );

error fun2( error );

virtualstd::ostream& print( std::ostream& )const;

private:

std::list<cx> cx_list_;// cx 是模版类型,既不是函数参数类型也不是函数返回值类型,所以cx.h 头文件不能删除

deduce deduce_d_;// deduce 是类型定义,也不删除他的头文件

};

inlinestd::ostream& operator<<( std::ostream& os,constold& old_val )

{returnold_val.print(os); }

 

到目前为止,删除了一些代码,是不是心情很爽,据说看一个程序员的水平有多高,不是看他写了多少代码,而是看他少写了多少代码。

如果你对C++编程有更深一步的兴趣,接下来的文字你还是会看的,再进一步删除代码,但是这次要另辟蹊径了

2.删除不必要的一大堆私有成员变量,转而使用”impl”方法

 

2.1.使用”impl”实现方式写代码,减少客户端代码的编译依赖

impl方法简单点说就是把类的私有成员变量全部放进一个impl类,然后把这个类的私有成员变量只保留一个impl*指针,代码如下

1

2

3

4

5

6

7

8

9

// file old.h

classold {

//公有和保护成员

// public and protected members

private:

//私有成员, 只要任意一个的头文件发生变化或成员个数增加,减少,所有引用old.h的客户端必须重新编译

// private members; whenever these change,

// all client code must be recompiled

};

 

改写成这样:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// file old.h

classold {

//公有和保护成员

// public and protected members

private:

classoldImpl* pimpl_;

// 替换原来的所有私有成员变量为这个impl指针,指针只需要前向声明就可以编译通过,这种写法将前向声明和定义指针放在了一起, 完全可以。

//当然,也可以分开写

// a pointer to a forward-declared class

};

// file old.cpp

structoldImpl {

//真正的成员变量隐藏在这里, 随意变化, 客户端的代码都不需要重新编译

// private members; fully hidden, can be

// changed at will without recompiling clients

};

 

不知道你看明白了没有,看不明白请随便写个类试验下,我就是这么做的,当然凡事也都有优缺点,下面简单对比下:

使用impl实现类 不使用impl实现类
优点 类型定义与客户端隔离,减少#include的次数,提高编译速度,库端的类随意修改,客户端不需要重新编译 直接,简单明了,不需要考虑堆分配,释放,内存泄漏问题
缺点 对于impl的指针必须使用堆分配,堆释放,时间长了会产生内存碎片,最终影响程序运行速度,每次调用一个成员函数都要经过impl->xxx()的一次转发 库端任意头文件发生变化,客户端都必须重新编译

改为impl实现后是这样的:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

// 只用 file and cx 有虚函数.

#include "file.h"

#include "db.h"

classcx;

classerror;

classold :publicfile,privatedb {

public:

old(constcx& );

db get_db(int,char* );

cx get_cx(int, cx );

cx& fun1( db );

error fun2( error );

virtualstd::ostream& print( std::ostream& )const;

private:

classoldimpl* pimpl;//此处前向声明和定义

};

inlinestd::ostream& operator<<( std::ostream& os,constold& old_val )

{returnold_val.print(os); }

//implementation file old.cpp

classoldimpl{

std::list<cx> cx_list_;

deduce dudece_d_;

};

 

3.删除不必要的类之间的继承

面向对象提供了继承这种机制,但是继承不要滥用,oldclass的继承就属于滥用之一,classold继承file和db类,继承file是公有继承,继承db是私有继承

,继承file可以理解,因为file中有虚函数,old要重新定义它,但是根据我们的假设,只有file和cx有虚函数,私有继承db怎么解释?!那么唯一可能的理由就是:

通过私有继承—让某个类不能当作基类去派生其他类,类似Java里final关键字的功能,但是从实例看,显然没有这个用意,所以这个私有继承完全不必要,应该改用包含的方式去使用db类提供的功能,这样就可以

把”db.h”头文件删除,把db的实例也可以放进impl类中,最终得到的类是这样的:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

// 只用 file and cx 有虚函数.

#include "file.h"

classcx;

classerror;

classdb;

classold :publicfile {

public:

old(constcx& );

db get_db(int,char* );

cx get_cx(int, cx );

cx& fun1( db );

error fun2( error );

virtualstd::ostream& print( std::ostream& )const;

private:

classoldimpl* pimpl;//此处前向声明和定义

};

inlinestd::ostream& operator<<( std::ostream& os,constold& old_val )

{returnold_val.print(os); }

//implementation file old.cpp

classoldimpl{

std::list<cx> cx_list_;

deduce dudece_d_;

};

小结一下:

这篇文章只是简单的介绍了减少编译时间的几个办法:

1.删除不必要的#include,替代办法使用前向声明(forwarddeclared)

2.删除不必要的一大堆私有成员变量,转而使用”impl”方法

3.删除不必要的类之间的继承

67 Responses to “ 减少C++代码编译时间的方法 ”

  1. best pron说道:

    ur9pVN Wow, fantastic blog format! How long have you been blogging for? you made running a blog look easy. The full look of your web site is excellent, as well as the content!

  2. wonderful points altogether, you just gained a brand new reader. What would you suggest about your post that you made a few days ago? Any positive?

  3. Muchos Gracias for your blog article.Really looking forward to read more. Fantastic.

  4. Way cool! Some extremely valid points! I appreciate you writing this article plus the rest of the website is extremely good.

  5. please take a look at the web pages we comply with, such as this one, as it represents our picks from the web

  6. Ep coc 250x250说道:

    Maybe you can write subsequent articles relating to this

  7. You have brought up a very excellent details , appreciate it for the post.

  8. cheap taxi说道:

    I will immediately grasp your rss feed as I can at to find your e-mail subscription hyperlink or newsletter service. Do you have any? Kindly allow me know in order that I may just subscribe. Thanks.

  9. browse说道:

    Valuable info. Lucky me I found your website by accident, and I am shocked why this accident did not happened earlier! I bookmarked it.

  10. Ellis Porter说道:

    Keep up the great work , I read few posts on this internet site and I believe that your blog is rattling interesting and contains bands of great information.

  11. Nwokolo Collins说道:

    Thanks so much for the blog.Really looking forward to read more. Great.

  12. Nwokolo说道:

    This is one awesome article.Really thank you! Great.

  13. san pham noel说道:

    Some genuinely wonderful posts on this website , thanks for contribution.

  14. It as hard to find experienced people for this subject, but you sound like you know what you are talking about! Thanks

  15. Landfill说道:

    There is evidently a bunch to identify about this. I consider you made various nice points in features also.

  16. Very good information. Lucky me I came across your website by chance (stumbleupon). I have book-marked it for later!

  17. Way cool! Some extremely valid points! I appreciate you penning this post and also the rest of the site is extremely good.

  18. I value the blog post.Really thank you! Much obliged.

  19. Really enjoyed this post.Really looking forward to read more. Really Great.

  20. web owners and bloggers made good content as you did, the

  21. Online说道:

    That is a really good tip especially to those fresh to the blogosphere. Simple but very accurate info Many thanks for sharing this one. A must read post!

  22. Supermodel Elena说道:

    This genuinely answered my predicament, thank you!

  23. I truly appreciate this post.Thanks Again. Great.

  24. David说道:

    Really enjoyed this blog.Really looking forward to read more. Really Great.

  25. Only a smiling visitant here to share the love (:, btw outstanding design. The price one pays for pursuing a profession, or calling, is an intimate knowledge of its ugly side. by James Arthur Baldwin.

  26. Im thankful for the blog.Really looking forward to read more. Want more.

  27. Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is fantastic, let alone the content!

  28. Crochet basico说道:

    This is one awesome blog.Really looking forward to read more. Great.

  29. Crochet basico说道:

    I think other site proprietors should take this website as an model, very clean and great user genial style and design, as well as the content. You are an expert in this topic!

  30. try this说道:

    Thanks again for the blog.Really thank you! Want more.

  31. white shoes说道:

    Merely wanna input that you have a very nice site, I the style it actually stands out.

  32. I think this is a real great post.Really thank you! Awesome.

  33. School news说道:

    Thanks for the article post.Really looking forward to read more. Great.

  34. School admission说道:

    I truly appreciate this article post.Much thanks again. Keep writing.

  35. There is evidently a bundle to identify about this. I think you made various good points in features also.

  36. What as Happening i am new to this, I stumbled upon this I ave found It absolutely useful and it has helped me out loads. I hope to contribute & help other users like its aided me. Good job.

  37. Major thankies for the blog article.Much thanks again. Cool.

  38. cay mai gia说道:

    This is one awesome blog post.Really looking forward to read more. Much obliged.

  39. dpboss online说道:

    Very good information. Lucky me I found your site by accident (stumbleupon). I ave bookmarked it for later!

  40. Real estate说道:

    Thanks a lot for the article.Really looking forward to read more. Fantastic.

  41. In general, the earlier (or higher ranked on the search results page)

  42. scary maze说道:

    It as on a completely different topic but it has pretty much the same page layout and design. Superb choice of colors!

  43. Major thankies for the blog article.Really looking forward to read more. Keep writing.

  44. You, my friend, ROCK! I found just the info I already searched everywhere and simply could not find it. What a great web-site.

  45. Major thankies for the post.Thanks Again. Great.

  46. Thanks so much for the blog post.Thanks Again. Keep writing.

  47. Really fantastic info can be found on site. The fundamental defect of fathers is that they want their children to be a credit to them. by Bertrand Russell.

  48. Muchos Gracias for your blog article. Keep writing.

  49. You made some decent points there. I did a search on the topic and found most persons will approve with your site.

  50. you ave gotten an excellent weblog here! would you wish to make some invite posts on my weblog?

  51. dclm devotionals说道:

    This is really fascinating, You are a very professional blogger. I ave joined your rss feed and sit up for searching for more of your great post. Also, I have shared your site in my social networks!

  52. Chasidy说道:

    Thanks for one as marvelous posting! I quite enjoyed reading it,

  53. best ev charger说道:

    welcome to wholesale mac makeup from us.

  54. see the website说道:

    please pay a visit to the internet sites we comply with, such as this one, because it represents our picks through the web

  55. This genuinely answered my challenge, thank you!

  56. We all speak a little about what you should speak about when is shows correspondence to simply because Maybe this has much more than one meaning.

  57. Way cool! Some extremely valid points! I appreciate you penning this article plus the rest of the website is really good.

  58. This very blog is really awesome as well as informative. I have discovered a lot of helpful things out of this blog. I ad love to visit it again and again. Thanks a lot!

  59. Very good article. I certainly appreciate this website. Stick with it!

  60. have a look at说道:

    Tremendous issues here. I am very glad to see your article. Thank you so much and I am looking forward to contact you. Will you please drop me a mail?

  61. Some truly interesting details you have written.Aided me a lot, just what I was searching for .

  62. in namecard说道:

    Im grateful for the blog.Really looking forward to read more. Awesome.

  63. Wow, incredible blog format! How lengthy have you been blogging for? you make running a blog glance easy. The full glance of your site is fantastic, as smartly the content material!

  64. You can definitely see your expertise in the work you write. The world hopes for even more passionate writers like you who are not afraid to say how they believe. Always go after your heart.

  65. check here说道:

    It as not that I want to replicate your internet site, but I really like the style and design. Could you let me know which design are you using? Or was it custom made?

  66. You made some decent points there. I looked on the internet for the subject and found most people will consent with your blog.

  67. Crista说道:

    Pretty! This has been an extremely wonderful article. Thank you for providing this information.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>