刀不磨会生锈,久了不编程,也会忘记很多关键的东西,其中之一就是会把日常编程中应当注意的问题忘记,慢慢地写的代码就很烂了,不忍卒读.....
编程是一门细致活儿,有很多陷阱,其中之一是我们容易编写劣化的代码,相当多的人都想不通自己写的代码怎么写都没有人家的执行起来有效率,所以在这里我就写一些自己掌握的这方面的知识,一来是防止自己忘却,二来是与大家分享,特别是初学编程者。
相当一部分人会认为我写出来的内容应该算是代码优化层面,我是不这样认为的。我认为编程中优化不可过早,但避免劣化代码却要时刻进行。我这里谈的内容有些是老生常谈,那我就深入挖掘,力求旧瓶有新酒;有些是鲜为人知,那我就详言细述,力求深入浅出。
关于避免代码劣化这个话题,将不限于C和C++两种语言,还包括python,lua等大家熟悉的脚本语言,甚至是boost,OpenMP等学用的库,也许还会见到SQL语句的优化等,希望大家多多捧场~
本系列文章欢迎转载、打印、分发等,但不可用于商业用途,任何时候必须保留全文完整,并声明转载自恋花蝶的博客(http://blog.csdn.net/lanphaday),谢谢。
避免劣化代码(一)
No Inferior Code,(1)
条款1,尽量使用拷贝构造函数初始化实例变量,而非使用赋值操作符进行初始化。
例:
#include <ctime>
#include <iostream>
#include <string>
void test_inferior_code(int cnt, const char* str)
{
for(int i = 0; i < cnt; ++i)
{
std::string s;
s = str;
s[0] = 'H';
}
}
void test_excellent_code(int cnt, const char* str)
{
for(int i = 0; i < cnt; ++i)
{
std::string s(str);
s[0] = 'H';
}
}
int main()
{
static const int cnt = 1000000;
char str[] = "hello";
clock_t bgn = clock();
test_inferior_code(cnt, str);
clock_t end = clock();
std::cout << "test_inferior_code used " << end-bgn << "ms\n";
bgn = clock();
test_excellent_code(cnt, str);
end = clock();
std::cout << "test_excellent_code used " << end-bgn << "ms\n";
return 0;
}
分析:
在我的机器(配置和编译执行环境见文后)上,上例的输出如下:
$ ./no_inferior_code1.exe
test_inferior_code used 4336ms
test_excellent_code used 3485ms
可以看到使用赋值操作符的版本执行时间是使用拷贝构造函数方式的1.2倍。这个数值并不算惊人,但绝不能不重视之;因为例子只是使用了std::string,如果是有多个元素的std::map呢,如果是用户自定义的类呢?所以勿以善小而不为,勿以恶小而为之,养成尽量使用拷贝构造函数的习惯非常重要。
上面的例子不太可能出现在我们的代码中,现实中最容易出现这种劣化代表的情况是这样的:
class A
{
public:
A(const std::string& str){_str = str;}
private:
std::string _str;
};
这样的代码很容易看到,其实就是一种劣化的代码。比较好的写法是这样的:
public:
A(const std::string& str):_str(str){}
使用构造列表来初始化成员变量。
扩展:
一路谈来都是使用STL等别人的库,那么如果自己要写一个类给其他人使用的时候,应该注意些什么吗?是的,我们应该提供能让他人写出excellent代码的接口或者方法。简单地说,如果我们提供的类重载了赋值操作符,那么就应该相应地提供一个拷贝构造函数;反过来,如果提供了拷贝构造函数,那也需要重载赋值操作符。如果你并不打算这样做,那一定要把你不提供的接口声明为private或者protected,以免编译器自动生成它,造成误会。
class A
{
public:
A(const A& a):_str(a._str){}
A& operator=(const A& a){_str = a.str;return *this;}
std::string str;
};
上面的代码是一个还算过得去的例子~
分享到:
相关推荐
INFERIOR
Major mode for Emacs for editing MATLAB code, and running MATLAB in an inferior shell.zip
组胺通过H1和H2受体兴奋大鼠前庭下核神经元,彭诗宇,庄乾兴,前庭下核是整合前庭信息,复合感觉和小脑信息传入的重要结构。本实验采用离体脑片的细胞外记录的方法,观察组胺对大鼠前庭下核
matlab一种图像分割代码视网膜层描绘 更新(2017 年 1 月 6 日):评估结果可用 [此处] ()(Pred 是变量名称)它对应于 [temp.mat] () (更新:Pretrained models_multiclass & models(只有trees as github forbids...
It is faster than GAs and has similar (only slightly inferior) performance. If you have been using PSO for some time, then you would definitely want to download the toolbox and modify the code to ...
网络试题资源库 Web版.rar网络试题资源库 Web版.rar
官方离线安装包,测试可用。请使用rpm -ivh [rpm完整包名] 进行安装
ukari no emacs配置 依存关系 sbcl 材料设计图标 源代码专业版 诺托·桑斯(Noto Sans)CJK SC 诺托·桑斯(Noto Sans)CJK JP 堆 glslangValidator 技术支持 语言 普通口齿不清 elisp 哈斯克尔 sl 尼克斯 yaml...
离线安装包,亲测可用
官方离线安装包,亲测可用
大多数代码取自 。 该自动完成源在REPL缓冲区(如下等inferior-python-mode等)中很有用。 截屏 安装 ac-capf在和ac-capf上可用 您可以使用以下命令安装ac-capf 。 MX软件包安装[RET] ac-capf [RET] 命令 ac-capf...
App Helpers交付测试助手传递测试使用的依赖项: mvc_pattern:^ 6.6.4 + 2 闪屏... Já no menu Restaurante, foi usado o json para trazer as informações. Também podemos verificar que no menu localização
antennas with no reduction in performance. In contrast if perfect channel-state information were available the power could be made inversely proportional to the number of antennas. Lower capacity ...
This book is intended primarily for readers who already have at least a basic ...omitted models are inferior, only that I have not found them to be outstandingly useful in my particular applications.
This book is intended primarily for readers who ... This does not imply that the omitted models are inferior, only that I have not found them to be outstandingly useful in my particular applications.
the selected debugger may be inappropriate for the inferior的解决方案
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
取代通常的( setq inferior-lisp-program " ecl " ) 和, ( setq inferior-lisp-program " gdb --eval-command=run --eval-command=quit --args ecl " ) 将在gdb下运行ecl ,这将为您提供具有c运
离线安装包,亲测可用
本设计是一个基于AT89S52单片机的日历显示系统,本设计能显示公历年、月、日,以及 时、分、秒、温度、星期等信息,而且还提供了农历信息,具有调整时间,温度采集,闹 钟及个性化的闹铃等功能。系统所用的时钟日历...