返回空字符串:C++ 中的有效方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26587110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:39:02  来源:igfitidea点击:

Returning an empty string : efficient way in c++

c++stringperformance

提问by Abhishek Chandel

I have 2 ways of returning an empty string from a function.

我有两种从函数返回空字符串的方法。

1)

1)

std::string get_string()
{
   return "";
}

2)

2)

std::string get_string()
{
   return std::string();
}

which one is more efficient and why?

哪个更有效,为什么?

回答by Baldrickk

Gcc 7.1 -O3 these are all identical, godbolt.org/z/a-hc1d – jterm Apr 25 at 3:27

Gcc 7.1 -O3 这些都是相同的,godbolt.org/z/a-hc1d – jterm 4 月 25 日 3:27

Original answer:

原答案:

Did some digging. Below is an example program and the relevant assembly:

做了一些挖掘。下面是一个示例程序和相关程序集:

Code:

代码:

#include <string>

std::string get_string1(){ return ""; }

std::string get_string2(){ return std::string(); }

std::string get_string3(){ return {}; }           //thanks  Kerrek SB

int main()
{
    get_string1();
    get_string2();
    get_string3();
}

Assembly:

集会:

__Z11get_string1v:
LFB737:
    .cfi_startproc
    pushl   %ebx
    .cfi_def_cfa_offset 8
    .cfi_offset 3, -8
    subl    , %esp
    .cfi_def_cfa_offset 48
    movl    48(%esp), %ebx
    leal    31(%esp), %eax
    movl    %eax, 8(%esp)
    movl    $LC0, 4(%esp)
    movl    %ebx, (%esp)
    call    __ZNSsC1EPKcRKSaIcE
    addl    , %esp
    .cfi_def_cfa_offset 8
    movl    %ebx, %eax
    popl    %ebx
    .cfi_restore 3
    .cfi_def_cfa_offset 4
    ret 
    .cfi_endproc

__Z11get_string2v:
LFB738:
    .cfi_startproc
    movl    4(%esp), %eax
    movl    $__ZNSs4_Rep20_S_empty_rep_storageE+12, (%eax)
    ret 
    .cfi_endproc

__Z11get_string3v:
LFB739:
    .cfi_startproc
    movl    4(%esp), %eax
    movl    $__ZNSs4_Rep20_S_empty_rep_storageE+12, (%eax)
    ret 
    .cfi_endproc

This was compiled with -std=c++11 -O2.

这是用-std=c++11 -O2.

You can see that there is quite a lot more work for the return "";statement and comparably little for return std::stringand return {};(these two are identical).

你可以看到,有相当多的大量的工作return "";说明和相当小的return std::stringreturn {};(这两个是相同的)。

As Frerich Raabe said, when passing an empty C_string, it still needs to do processing on it, instead of just allocating memory. It seems that this can't be optimised away (at least not by GCC)

正如 Frerich Raabe 所说,当传递一个 empty 时C_string,它仍然需要对其进行处理,而不仅仅是分配内存。似乎这无法优化(至少不是 GCC)

So the answer is to definitely use:

所以答案是肯定使用:

return std::string();

or

或者

return {};   //(c++11)

Although unless you are returning a lotof empty strings in performance critical code (logging I guess?), the difference is going to still be insignificant.

尽管除非您在性能关键代码中返回大量空字符串(我猜是日志记录?),但差异仍然是微不足道的。

回答by Frerich Raabe

The latter version is never slower than the first. The first version calls the std::stringconstructor taking a C string, which then has to compute the length of the string first. Even though that's fast to do for an empty string, it's certainly not faster than not doing it at all.

后一个版本永远不会比第一个慢。第一个版本调用std::string带有 C 字符串的构造函数,然后它必须首先计算字符串的长度。尽管这对于空字符串来说很快,但它肯定不会比根本不这样做快。