Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an empty string : efficient way in c++

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

1)

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

2)

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

which one is more efficient and why?

like image 773
Abhishek Chandel Avatar asked Oct 27 '14 11:10

Abhishek Chandel


People also ask

How do I completely empty a string in C?

If you want to zero the entire contents of the string, you can do it this way: memset(buffer,0,strlen(buffer));

What does string empty return?

Since string is a language alias for String , there is absolutely no difference between the first and the second expressions. Starting with . NET 2, "" returns exactly the same object as String. Empty , making all three statements exact equivalents of each other in terms of the value that they return.

Is string empty faster?

Consider Real World Optimization String x = ""; In my test, there was no noticeable difference. Sometimes string. Empty was faster and sometimes the empty string was faster.


2 Answers

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.

like image 158
Baldrickk Avatar answered Sep 20 '22 06:09

Baldrickk


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.

like image 38
Frerich Raabe Avatar answered Sep 20 '22 06:09

Frerich Raabe