-1

Why the former is more optimal than the latter (answer preferable in plain English)?

Why this method is better

str = "hello ";
str2 = "world";
str += str2;

than

str = "hello ";
str2 = "world";
str = str + str2;
ibrabbit
  • 43
  • 5
  • Where did you see it's "better"? – SOFe Aug 13 '19 at 03:44
  • 7
    Think about what `str + str2` returns – M.M Aug 13 '19 at 03:44
  • 1
    read more here https://stackoverflow.com/q/12479486/10858827 – Arne Aug 13 '19 at 03:53
  • 4
    `str + str2` returns a temporary that it's moved (in older compilers could even be copied) into `str`. There are therefore 3 strings in use (`str`, `str2`, and the temporary). In `str+= str2` you're doing the equivalent of `str.append(str2)`, which not only has 2 strings in use, but also if `str` buffer is large enough, it doesn't need to reallocate the buffer memory for `str` – Mirko Aug 13 '19 at 04:06
  • 2
    Your "more optimal" premise is wrong, as the two approaches might be equally optimal in practice. Answers to a question based on a false assumption are not necessarily meaningful. Also, "better" is a subjective term; please specify in which way one method is supposedly better (e.g. faster? more readable? more correct? etc.). – JaMiT Aug 13 '19 at 04:17
  • @Mirko str and str2 are both literals, Does the expression (str + str2) return something? I always had the idea of methods returning something. Do expressions have return value as well? – ibrabbit Aug 13 '19 at 05:26
  • @JaMiT in a quicker way. – ibrabbit Aug 13 '19 at 05:29
  • An expression (str + str2) does, of course, return something. If it wouldn't - how could you assign the result of expression to something else? (Or how could you use expressions as function arguments?) Furthermore, the type of each operator is well defined and depending on both arguments. E.g. `operator+(const std::string&, const std::string&)` returns `std::string`. Assignment `std::string::operator+=(const std::string&)` returns `std::string&`. (The returned value is `*this`.) This becomes obvious when operators are overloaded (what's actually the case for `std::string`). – Scheff's Cat Aug 13 '19 at 05:50
  • Doc. to read more: [`operator+(const std::string&, const std::string)`](https://en.cppreference.com/w/cpp/string/basic_string/operator%2B) and [`std::string::operator+=(const std::string&)`](https://en.cppreference.com/w/cpp/string/basic_string/operator%2B%3D). In both cases, there are additional operators with alternative parameter types. (Concerning operator overloading: assignment operators like `+=` have to be class members while other binary operators like `+` can be class members or free-standing functions, and the latter is recommended.) – Scheff's Cat Aug 13 '19 at 05:56
  • Sorry. _depending on both arguments_ should be _depending on all arguments_. _both_ is correct for binary operators... ;-) – Scheff's Cat Aug 13 '19 at 05:59
  • _str and str2 are both literals_ IMHO, this is not quite true. Actually, it's not clear from your exposed sample what they actually are. May be, you declared `std::string str, str2;`. In this case, `str` and `str2` are variables of type `std::string`. Assigning a literal `"hello"` or `"world"` doesn't change this. (Please, note that [`std::string::operator=(const char*)`](https://en.cppreference.com/w/cpp/string/basic_string/operator%3D) is used for assignment of `str` and `str2` in this case.) – Scheff's Cat Aug 13 '19 at 06:06
  • Alternatively, you could have declared also `const char *str, *str2;` but I doubt that you did. Why? There is no `operator+(const char*, const char*)` (and AFAIK it's impossible to define one). So, in this case `str + str2` would cause a compiler error. – Scheff's Cat Aug 13 '19 at 06:07
  • @Scheff thanks a lot. Btw, I perceived return as a return value(same as what functions return), thanks for clarifying :). – ibrabbit Aug 13 '19 at 06:25

1 Answers1

2

The question here is, what better means. Let us assume, less complex assembler code and maybe an advantage in speed and a little bit in size.

It also depends a little bit on the qualifiers of the variable and very much on the compiler and the compiler options.

The naive approach and understanding is always: For x = x + y;, at first the result of the addition will be calculated and then this result (a temporary) will be assigned to the original variable. For x += y the string y will directly be appended to x.

In case of strings, the addition is implemented by calling the strings append function.

And, as said, if you do not switch on compiler optimization, there will first be an append operation and then an assignment. You may check the generated assembler code. So the x = x + y is indeed more complex than the x += y. However, if you switch on compiler optimizations (e.g. -O3 for gcc), there will be no difference.

There maybe other interpretations for "better". More readable, or better maintainable, or fitting to some coding guidelines or some operator overload constraints. So somehow hard to give a convrete answer and the result would maybe be opinion based.

A M
  • 14,694
  • 5
  • 19
  • 44
  • so the command line would be gcc file.c -O3. and what for c++? And why did u say result a temporary? – ibrabbit Aug 13 '19 at 08:50
  • Yes, same parameter "-O3" for gcc or g++. Temporary is necessary to store the intermediate result somehwere (not visible to you). Then take this intermediate result and assign it to the destination string. Addition and assignment are 2 different operations. – A M Aug 13 '19 at 08:54
  • oh! I get it now. Thanks a lot, bro :). – ibrabbit Aug 13 '19 at 09:35