gpt4 book ai didi

visual-c++ - 要求对 VC++ 进行有关插入和安放的调查

转载 作者:行者123 更新时间:2023-12-04 06:18:50 24 4
gpt4 key购买 nike

我正在调查 vector 的以下成员函数中执行了多少特殊成员函数:

insert
emplace
push_back
emplace_back

在各种运行时间条件下。我的目的是发表一篇论文,将这些成员函数相互比较,以及 std::lib 的每个 c++11 实现:libstdc++、libc++ 和 vc++,并就何时最好提供建议使用每个成员函数(当有多个选择时)。我有关于 libc++ 和 libstdc++-4.8 的数据,但我缺少关于 vc++ 的数据。

我正在寻找有关 vc++ 的答案,其中包括 vc++ 版本号(2010、2012、2013)。我的目的是就何时应该优先考虑这些成员中的每一个,并根据现实世界的经验提供建议。我将对每个 vc++ 版本的第一个响应进行投票。我的意图不是抨击任何特定平台。但是,如果我的结果鼓励任何现有平台在 future 改进,那就太糟糕了。

我想看到的测试结果是:
#include <iostream>
#include <vector>

class X
{
int i_;
int* p_;

public:
struct special
{
unsigned c;
unsigned dt;
unsigned cc;
unsigned ca;
unsigned mc;
unsigned ma;
};
static special sp;

X(int i, int* p)
: i_(i)
, p_(p)
{
// std::cout << "X(int i, int* p)\n";
sp.c++;
}

~X()
{
// std::cout << "~X()\n";
sp.dt++;
}

X(const X& x)
: i_(x.i_)
, p_(x.p_)
{
// std::cout << "X(const X& x)\n";
sp.cc++;
}

X& operator=(const X& x)
{

i_ = x.i_;
p_ = x.p_;
// std::cout << "X& operator=(const X& x)\n";
sp.ca++;
return *this;
}

X(X&& x) noexcept
: i_(x.i_)
, p_(x.p_)
{
// std::cout << "X(X&& x)\n";
sp.mc++;
}

X& operator=(X&& x) noexcept
{

i_ = x.i_;
p_ = x.p_;
// std::cout << "X& operator=(X&& x)\n";
sp.ma++;
return *this;
}

};

std::ostream&
operator<<(std::ostream& os, X::special const& sp)
{
os << sp.c << '\n';
os << sp.dt << '\n';
os << sp.cc << '\n';
os << sp.ca << '\n';
os << sp.mc << '\n';
os << sp.ma << '\n';
return os;
}

X::special X::sp{};

int
main()
{
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert lvalue no reallocation--\n";
X::sp = {};
v.insert(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace lvalue no reallocation--\n";
X::sp = {};
v.emplace(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert xvalue no reallocation--\n";
X::sp = {};
v.insert(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace xvalue no reallocation--\n";
X::sp = {};
v.emplace(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--insert rvalue no reallocation--\n";
X::sp = {};
v.insert(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace rvalue no reallocation--\n";
X::sp = {};
v.emplace(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert lvalue reallocation--\n";
X::sp = {};
v.insert(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace lvalue reallocation--\n";
X::sp = {};
v.emplace(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert xvalue reallocation--\n";
X::sp = {};
v.insert(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace xvalue reallocation--\n";
X::sp = {};
v.emplace(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--insert rvalue reallocation--\n";
X::sp = {};
v.insert(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace rvalue reallocation--\n";
X::sp = {};
v.emplace(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}

{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back lvalue no reallocation--\n";
X::sp = {};
v.push_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back lvalue no reallocation--\n";
X::sp = {};
v.emplace_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back xvalue no reallocation--\n";
X::sp = {};
v.push_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back xvalue no reallocation--\n";
X::sp = {};
v.emplace_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--push_back rvalue no reallocation--\n";
X::sp = {};
v.push_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace_back rvalue no reallocation--\n";
X::sp = {};
v.emplace_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back lvalue reallocation--\n";
X::sp = {};
v.push_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back lvalue reallocation--\n";
X::sp = {};
v.emplace_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back xvalue reallocation--\n";
X::sp = {};
v.push_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back xvalue reallocation--\n";
X::sp = {};
v.emplace_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--push_back rvalue reallocation--\n";
X::sp = {};
v.push_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace_back rvalue reallocation--\n";
X::sp = {};
v.emplace_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
}

例如,这里是 gcc-4.8 上这个测试的输出: https://ideone.com/imAMnU

如果有人发现 4.8 之后的 gcc 版本的结果不同并且想要提供这些结果,那也将受到赞赏(并赞成)。如果我无法区分哪些等效的响应首先出现,我很高兴为所有这些响应提供支持。但是,此时我不确定如何为这项调查选择正确的答案。

在我可能发表的任何内容中,数据的贡献者肯定会得到感谢。我也很高兴在这个问题中发布一个链接,指向将来可能从这些数据中得出的任何结论。

总之,我意识到这种风格的问题不是 SO 的常态。然而,它是本着促进 C++ 社区的公共(public)知识的精神提出的,并且尽可能地透明。如果人们出于任何原因想要结束这个问题,我接受这一点,但我个人认为这不符合 C++ 社区的最佳利益。

更新

这是我所说的使这项调查成为可能的论文:

insert vs emplace

最佳答案

使用 Release 模式编译的 VS2012 代码。

命令

cl/O2/EHsc/W4 Console_12.cpp

输出

--insert lvalue no reallocation--    
4
4
1
3
4
0
----
--emplace lvalue no reallocation--
8
15
2
3
10
6
----
--insert xvalue no reallocation--
12
26
2
3
17
12
----
--emplace xvalue no reallocation--
16
37
2
3
24
18
----
--insert rvalue no reallocation--
20
49
2
3
31
24
----
--emplace rvalue no reallocation--
24
60
2
3
38
30
----
--insert lvalue reallocation--
28
70
3
3
44
30
----
--emplace lvalue reallocation--
32
84
4
3
53
36
----
--insert xvalue reallocation--
36
98
4
3
63
42
----
--emplace xvalue reallocation--
40
112
4
3
73
48
----
--insert rvalue reallocation--
44
127
4
3
83
54
----
--emplace rvalue reallocation--
48
141
4
3
93
60
----
--push_back lvalue no reallocation--
52
148
5
3
96
60
----
--emplace_back lvalue no reallocation--
56
156
6
3
99
60
----
--push_back xvalue no reallocation--
60
164
6
3
103
60
----
--emplace_back xvalue no reallocation--
64
172
6
3
107
60
----
--push_back rvalue no reallocation--
68
181
6
3
111
60
----
--emplace_back rvalue no reallocation--
72
189
6
3
115
60
----
--push_back lvalue reallocation--
76
199
7
3
121
60
----
--emplace_back lvalue reallocation--
80
210
8
3
127
60
----
--push_back xvalue reallocation--
84
221
8
3
134
60
----
--emplace_back xvalue reallocation--
88
232
8
3
141
60
----
--push_back rvalue reallocation--
92
244
8
3
148
60
----
--emplace_back rvalue reallocation--
96
255
8
3
155
60
----

关于visual-c++ - 要求对 VC++ 进行有关插入和安放的调查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24925004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com