Returning an empty string : efficient way in c++
Gcc 7.1 -O3 these are all identical, godbolt.org/z/a-hc1d – jterm Apr 25 at 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 $40, %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 $40, %esp
.cfi_def_cfa_offset 8
movl %ebx, %eax
popl %ebx
.cfi_restore 3
.cfi_def_cfa_offset 4
ret $4
.cfi_endproc
__Z11get_string2v:
LFB738:
.cfi_startproc
movl 4(%esp), %eax
movl $__ZNSs4_Rep20_S_empty_rep_storageE+12, (%eax)
ret $4
.cfi_endproc
__Z11get_string3v:
LFB739:
.cfi_startproc
movl 4(%esp), %eax
movl $__ZNSs4_Rep20_S_empty_rep_storageE+12, (%eax)
ret $4
.cfi_endproc
This was compiled with -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::string
and return {};
(these two are identical).
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)
So the answer is to definitely use:
return std::string();
or
return {}; //(c++11)
Although unless you are returning a lot of empty strings in performance critical code (logging I guess?), the difference is going to still be insignificant.
The latter version is never slower than the first. The first version calls the std::string
constructor 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.