这是一个你应该真正测量的情况。
而且我看OP的副本赋值运算符,看到效率低下:
A& operator=(A const& other)
{A temp = other; std::swap(*this, temp); return *this;}
如果*this
和other
具有相同的s
?
在我看来,如果s == other.s
更聪明的复制分配可以避免访问堆。所有这将要做的就是复制:
A& operator=(A const& other)
{
if (this != &other)
{
if (s != other.s)
{
delete [] p;
p = nullptr;
s = 0;
p = new int[other.s];
s = other.s;
}
std::copy(other.p, other.p + s, this->p);
}
return *this;
}
如果不需要强异常安全,在拷贝赋值只有基本的异常安全(就像std::string
,std::vector
等),然后有一个上述潜在的性能改进。多少?测量。
我编写这个类三种方式:
设计1:
使用上述拷贝赋值运算符和OP的举动赋值操作符1#。
设计2:
使用上述拷贝赋值运算符和OP的举动赋值操作符2#。
设计3:
DeadMG的两个副本拷贝赋值运算符和移动分配。
这里是我用来测试的代码:
#include <cstddef>
#include <algorithm>
#include <chrono>
#include <iostream>
struct A
{
std::size_t s;
int* p;
A(std::size_t s) : s(s), p(new int[s]){}
~A(){delete [] p;}
A(A const& other) : s(other.s), p(new int[other.s])
{std::copy(other.p, other.p + s, this->p);}
A(A&& other) : s(other.s), p(other.p)
{other.s = 0; other.p = nullptr;}
void swap(A& other)
{std::swap(s, other.s); std::swap(p, other.p);}
#if DESIGN != 3
A& operator=(A const& other)
{
if (this != &other)
{
if (s != other.s)
{
delete [] p;
p = nullptr;
s = 0;
p = new int[other.s];
s = other.s;
}
std::copy(other.p, other.p + s, this->p);
}
return *this;
}
#endif
#if DESIGN == 1
// Move assignment operator #1
A& operator=(A&& other)
{
swap(other);
return *this;
}
#elif DESIGN == 2
// Move assignment operator #2
A& operator=(A&& other)
{
delete [] p;
s = other.s;
p = other.p;
other.s = 0;
other.p = nullptr;
return *this;
}
#elif DESIGN == 3
A& operator=(A other)
{
swap(other);
return *this;
}
#endif
};
int main()
{
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::duration<float, std::nano> NS;
A a1(10);
A a2(10);
auto t0 = Clock::now();
a2 = a1;
auto t1 = Clock::now();
std::cout << "copy takes " << NS(t1-t0).count() << "ns\n";
t0 = Clock::now();
a2 = std::move(a1);
t1 = Clock::now();
std::cout << "move takes " << NS(t1-t0).count() << "ns\n";
}
下面是我得到的输出:
$ clang++ -std=c++11 -stdlib=libc++ -O3 -DDESIGN=1 test.cpp
$ a.out
copy takes 55ns
move takes 44ns
$ a.out
copy takes 56ns
move takes 24ns
$ a.out
copy takes 53ns
move takes 25ns
$ clang++ -std=c++11 -stdlib=libc++ -O3 -DDESIGN=2 test.cpp
$ a.out
copy takes 74ns
move takes 538ns
$ a.out
copy takes 59ns
move takes 491ns
$ a.out
copy takes 61ns
move takes 510ns
$ clang++ -std=c++11 -stdlib=libc++ -O3 -DDESIGN=3 test.cpp
$ a.out
copy takes 666ns
move takes 304ns
$ a.out
copy takes 603ns
move takes 446ns
$ a.out
copy takes 619ns
move takes 317ns
DESIGN 1
看起来相当不错。警告:如果类需要“快速”释放资源,例如互斥锁的所有权或文件的开放状态所有权,则从正确性的角度来看,设计2移动赋值运算符可能会更好。但是,当资源只是内存时,通常有利的是尽可能延迟释放资源(如OP的用例)。
注意事项2:如果您还有其他用例,您知道它们很重要,请测量它们。你可能得出不同的结论,比我在这里。
注:我比“DRY”更看重表现。这里的所有代码将被封装在一个类中(struct A
)。使struct A
尽可能好。如果你做的工作质量足够高,那么你的客户struct A
(可能是你自己)不会被诱惑到“RIA”(再次重塑它)。我更喜欢在一个类中重复一个小代码,而不是一遍又一遍地重复执行整个类。
我不完全明白你的问题。你为什么不简单地使用'std :: unique_ptr'成员(而不是'int *'),并让所讨论的操作符是自动生成的还是'= default'? – Walter 2015-11-19 09:12:30
@Walter:问题是一个学习实验,而不是我在生产中使用的东西。我会选择'std :: vector'来代替。另外,在撰写本文时,默认值并未由MSVC实现。 – 2015-11-19 10:22:14
不够公平,但'MSVC'不在标签中。 – Walter 2015-11-19 13:40:07