STD :: String是否使用Malloc(免费,REALLOC等)函数家族来分配数据?
考虑使用自定义字符串类附加操作员的实现:
inline string& operator+=(const string& rhs){
if(rhs.size+(size-1)>capacity) {
data = static_cast<char8_t *>(realloc(data, capacity+=rhs.capacity));
}
//culprit???
memcpy((data + size - 1), rhs.data,rhs.size);
size+=rhs.size;
return *this;
}
数据是动态分配的内存,大小和容量是成员变量。看起来很标准吗? 的STD ::字符串对此进行基准测试
std::string test_a,test_b; test_a = "efjrpojgerojoejr"; test_b = "1204r94f3212345";
unsigned long long average_std = 0;
unsigned long long average_custom = 0;
{
for(int i = 0; i < 1000000; ++i) {
std::string a(test_a);
std::string b(test_b);
auto start = high_resolution_clock::now();
a += b;
auto stop = high_resolution_clock::now();
std::cout << a << std::endl;
average_std+= (stop - start).count();
}
}
{
for(int i = 0; i < 1000000; ++i) {
custom::string a(test_a);
custom::string b(test_b);
auto start = high_resolution_clock::now();
a += b;
auto stop = high_resolution_clock::now();
std::cout << a << std::endl;
average_custom += (stop - start).count();
}
}
std::cout << ((double)(average_custom/1000000))/((double)(average_std/1000000)) << std::endl;
我尝试使用So:STD :: string始终〜50% 。我以为罪魁祸首可能是纪念性的,我将其完全评论(显然使附录失败),但这对运行时没有区别。因此,我的结论是,Realloc是这里的罪魁祸首(我用来基准测试的示例使得STD :: String :: string and custom :: String reallocate)。我试图理解库代码,但在那里并没有太远。那么STD :: string string sigation sikate sig otal of dodoo魔术呢?预先分配的内存或其他东西?请注意,这是在具有-O3优化的Intel MacBook上的Clang 12.0.0上。
Consider this implementation of a custom string class append operator:
inline string& operator+=(const string& rhs){
if(rhs.size+(size-1)>capacity) {
data = static_cast<char8_t *>(realloc(data, capacity+=rhs.capacity));
}
//culprit???
memcpy((data + size - 1), rhs.data,rhs.size);
size+=rhs.size;
return *this;
}
where data is the dynamically allocated memory and size and capacity are member variables. Looks pretty standard right? I tried benchmarking this against std::string like so:
std::string test_a,test_b; test_a = "efjrpojgerojoejr"; test_b = "1204r94f3212345";
unsigned long long average_std = 0;
unsigned long long average_custom = 0;
{
for(int i = 0; i < 1000000; ++i) {
std::string a(test_a);
std::string b(test_b);
auto start = high_resolution_clock::now();
a += b;
auto stop = high_resolution_clock::now();
std::cout << a << std::endl;
average_std+= (stop - start).count();
}
}
{
for(int i = 0; i < 1000000; ++i) {
custom::string a(test_a);
custom::string b(test_b);
auto start = high_resolution_clock::now();
a += b;
auto stop = high_resolution_clock::now();
std::cout << a << std::endl;
average_custom += (stop - start).count();
}
}
std::cout << ((double)(average_custom/1000000))/((double)(average_std/1000000)) << std::endl;
std::string is consistently ~50% faster. Thinking that the culprit might be memcpy, I commented it out completely(which obviously makes the append fail) but it made no difference to runtime. So my conclusion is that realloc is the culprit here(the example I used to benchmark is such that both std::string and custom::string reallocate). I tried making sense of the library code but I didn't get very far there. So what voodoo magic does std::string to allocate so fast? A pre-allocated block of memory or something? Note this was on clang 12.0.0 on an intel macbook with -O3 optimization.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论