gpt4 book ai didi

c++ - 如何实现STL容器中对象的快速释放?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:07:43 25 4
gpt4 key购买 nike

TL/DR: How can a large std::vector<std::string> achieve such fast deallocation when compiled with release settings on Visual Studio 2012 RC?

我写了一个类strung其行为类似于 std::string作为练习,实现基本的复制和移动语义。

class strung
{
private:
size_t length_;
char* data_;

public:
// -------- Constructors --------

strung() : length_(0), data_(nullptr) {};

strung(const char* c_str)
{
length_ = strlen(c_str);
data_ = new char[length_];
::std::copy(c_str, c_str + length_, data_);
};

inline explicit strung(size_t length) : length_(length)
{
data_ = new char[length_];
};

strung(size_t length, char value) : length_(length)
{
data_ = new char[length_];
::std::fill(data_, data_ + length_, value);
};

// -------- Copy/move-constructors --------

strung(const strung& old)
{
data_ = new char[old.length_];
::std::copy(old.data_, old.data_ + old.length_, data_);
length_ = old.length_;
};

strung(strung&& old)
{
data_ = old.data_;
length_ = old.length_;
// Even though it is a rvalue, its destructor will still be called,
// so we would like to prevent our data from being freed.
old.data_ = nullptr;
};

// -------- Assignment operators --------

inline strung & operator =(const strung& old)
{
if (this != &old)
{
delete[] data_;
data_ = new char[old.length_];
::std::copy(old.data_, old.data_ + old.length_, data_);
length_ = old.length_;
}
return *this;
};

strung & operator =(strung&& old)
{
if (this != &old)
{
delete[] data_;
data_ = old.data_;
length_ = old.length_;
old.data_ = nullptr;
}
return *this;
};

// -------- Array operators (no bounds checking by design) --------

inline char& operator[](size_t pos)
{
return data_[pos];
};

inline const char& operator[](size_t pos) const
{
return data_[pos];
};

// -------- Insertion operator for `ostream`s --------

inline friend ::std::ostream &operator<<(::std::ostream &out, const strung& source)
{
out.write(source.data_, source.length_);
return out;
};

// -------- Various functions --------

inline const size_t length() const
{
return length_;
}

// -------- Poor man's iterators --------

char* begin()
{
return data_;
};

char* end()
{
return data_ + length_;
};

// -------- Destructor --------

inline ~strung()
{
delete[] data_;
};
};

我尝试比较 std::string 的性能和 strung使用此代码:

double time(const std::function<void(void)> &func)
{
using namespace std::chrono;
auto t1 = high_resolution_clock::now();
func();
auto total = duration_cast<nanoseconds>(high_resolution_clock::now()-t1);
return static_cast<double>(total.count()) / 1000000.;
}

template<typename T>
void test(const int num)
{
double allocation_time, full_time;

full_time = time([&] {
std::vector<T> container;

allocation_time = time([&] {
container.reserve(num);

for (int i=0; i < num; i++)
{
container.emplace_back(rand() % 10 + 1,'\0');

for (char &chr : container.back())
chr = ('A' + rand() % ('Z' - 'A' + 1) );
}
});
});

std::cout << "Full time: " << full_time << " miliseconds" << std::endl
<< "Allocation time: " << allocation_time << " miliseconds" << std::endl
<< "Deallocation time: " << full_time - allocation_time << " miliseconds" << std::endl;
}

int main()
{
std::cout << "-------- std::string --------" << std::endl;
test<std::string>(500000);
std::cout << "-------- strung --------" << std::endl;
test<strung>(500000);
return EXIT_SUCCESS;
}

结果如下:

调试 (x86-64)

-------- std::string --------
Full time: 51050.9 miliseconds
Allocation time: 1853.11 miliseconds
Deallocation time: 49197.8 miliseconds
-------- strung --------
Full time: 52404 miliseconds
Allocation time: 4886.28 miliseconds
Deallocation time: 47517.7 miliseconds

版本 (x86-64):

-------- std::string --------
Full time: 113.007 miliseconds
Allocation time: 107.006 miliseconds
Deallocation time: 6.0004 miliseconds
-------- strung --------
Full time: 47771.7 miliseconds
Allocation time: 356.02 miliseconds
Deallocation time: 47415.7 miliseconds

分配速度是可以理解的,因为我并没有真正对类进行太多优化,但释放速度更有趣。

对调试设置的测试表明,对于 std::string 两者,重新分配同样复杂。和 strung (虽然仍然很慢),但是对发布设置的测试会为 std::string 重新分配非常非常快,同时 strung保持完全相同。什么是std::string考虑到strung,实现如此快速的释放的析构函数几乎是微不足道的。

起初我以为std::string被优化为 nop,所以根本不执行释放,但是当我删除 strung 时的析构函数,后者仍然快得多,所以情况可能并非如此。

我希望我的释放速度更快,那么我该怎么做才能达到类似的释放速度?

最佳答案

Microsoft 的 std::string 实现使用了一种叫做“小字符串优化”的东西。这意味着 std::string 实际上包含一个 15 个字符的字符串(char[16])。如果给定一个短于 16 个字符的字符串,那么它会将其存储在该内存中。所以在这些情况下没有进行动态内存分配。

您的strung 总是 动态分配字符串。这意味着它的析构函数将始终释放它。 std::string,如果足够小,则两者都不做。

关于c++ - 如何实现STL容器中对象的快速释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12131157/

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