【分析】

i++与++i哪个效率更高?

(1)在内建数据类型的情况下,效率没有区别;

(2)在自定义数据类型Class的情况下,++i效率更高!

自定义数据类型的情况下:++i返回对象的引用;i++总是要创建一个临时对象,在退出函数时还要销毁它,而且返回临时对象的值时还会调用其拷贝构造函数。

【代码】

 C++ Code 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;

class Integer
{
public:
    Integer(int value): v(value)
    {
        cout << "default constructor" << endl;
    }
    Integer(const Integer &other)
    {
        cout << "copy constructor" << endl;
        v = other.v;
    }
    Integer &operator=(const Integer &other)
    {
        cout << "copy assignment" << endl;
        v = other.v;
        return *this;
    }

// ++i  first +1,then return new value
    Integer &operator++()
    {
        cout << "Integer::operator++()" << endl;
        v++;
        return *this;
    }

// i++  first save old value,then +1,last return old value
    Integer operator++(int)
    {
        cout << "Integer::operator++(int)" << endl;
        Integer old = *this;
        v++;
        return old;
    }

void output()
    {
        cout << "value " << v << endl;
    }
private:
    int v;
};

void test_case()
{
    Integer obj();
    Integer obj2 = obj;
    Integer obj3();
    obj3 = obj;
    cout << "--------------" << endl;
    cout << "++i" << endl;
    ++obj;
    obj.output();
    cout << "i++" << endl;
    obj++;
    obj.output();
}

int main()
{
    test_case();
    ;
}
/*
default constructor
copy constructor
default constructor
copy assignment
--------------
++i
Integer::operator++()
value 11
i++
Integer::operator++(int)
copy constructor
value 12
*/

随机推荐

  1. c语言游戏推箱子

    前两天做了推箱子小游戏,看似简单的一个小游戏背后却 有巨大的秘密,这秘密就是一大堆逻辑. 自从学习了函数过后,的确是解决了很多问题,而且调用很方便,尽管我现在都不是很会调用. 写完一个函数,准备测试一 ...

  2. break prefab instance的原理

    prefab可无限apply: 如果把一个模块做成了prefab,这个prefab可能在同一个scene中添加多个,甚至添加到了多个scene中.设所有这些实例为instance(1),instanc ...

  3. linux 安全狗

    下载 ;安装; service safedog start  或者 sdstart;sdui

  4. js小数点失精算法修正

    在用js计算0.07*100时候竟然=7.000000000000001 关于js失精算法你都遇到哪些,让我们一起来细数一下吧 console.log(0.07*100); // 7.00000000 ...

  5. MySQL数据库备份--mysqldump用法

    导出要用到MySQL的mysqldump工具,基本用法是:   shell> mysqldump [OPTIONS] database [tables] 如果你不给定任何表,整个数据库将被导出. ...

  6. scala速成记录1

    选择  Learning Scala这本书,两百多页,足够薄. 安装 http://www.scala-lang.org/  下载Binary的版本.bin里边有所有操作系统下运行的可以运行的交互式s ...

  7. Apache配置手札

    一.绑定域名到子目录 在httpd.conf文件末尾添加 #不同的域名对应到的目录 <VirtualHost *:80> DocumentRoot "D:\wamp\www\ba ...

  8. JavaScript----Js操控-HTML5 <progress> 标签

    Js操控----HTML5 <progress> 标签 简单模拟下下载进度跑条 <h4>加载进度</h4> <input type="button& ...

  9. [转]Eclipse启动Tomcat时45秒超时解决方法

    原文地址:http://it.oyksoft.com/post/6577/ Eclipse启动Tomcat时,默认配置的启动超时时长为45秒.假若项目启动超过45秒将会报错.两种解决方法:1.改XML ...

  10. .NET 学习书籍推荐

    时间过得好快啊,转眼三月过了1周多了,今天把看书的目录记录下,方便初学者 最近由于项目需要研究下dotnet 技术,参考书籍如下: 1.深入理解C# 2.CLR via  C# 3.Framework ...