4

I am coding in C++. Let s be some string. I am asked to determine which of the following is faster:

cout << "s:"  + s  + "s:"  + s  + " s:"  + s  + "\n";

cout << "s:" << s << "s:" << s << " s:" << s << "\n";

I ran both of them repeatedly to find that the second one is faster. I spent a while trying to figure out why. I think it is because in the first one, the string is first concatenated then output to the screen. But the second one just output straight to the screen. Is that correct?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
user44322
  • 351
  • 3
  • 8
  • 1
    The 'homework' tag is deprecated: http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated – jogojapan Nov 13 '12 at 03:08

4 Answers4

4

The first will likely involve a few memory allocations for the string concatenations, followed by the copying of the final concatenated string to the output buffer. The second one will simply copy already allocated string data to an already allocated output buffer.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
0

From a theoretical perspective the second example is linear time while the first one can be quadratic time (in the number of substrings), depending on the implementation.

To determine whether that is the case with your implementation, you will have to look at the source code and (because the compiler might optimize thing) at the machine code.

In short, the reasons why depend on the implementation, and in general, to determine "which is fastest", you have no option but to MEASURE. The "reasons why" can act as heuristic guidelines. But that's all: in the end it's measurements, reality, that counts.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • This is not just measurable, its provable in the code, stdout is not going to hit constraints that buffer creations will while concatenating strings. – Michael Nov 13 '12 at 03:23
  • @Michael: It's unclear what you mean is "provable", but almost nothing is provable from the source code wrt. the OP's question. In particular, some C++ compilers with certain options, optimize string concatenation, and with some library implementations each call of `<<` is a performance drain. So in general you just have to measure. – Cheers and hth. - Alf Nov 13 '12 at 04:07
0

Your hypothesis that the second is faster due to the first creating string objects is likely correct. The key here is "likely". These are std library functions and as such can have different implementation details as the standard defines behavior and not how it is implemented. In theory, you could find a standard library implementation where the opposite of your findings is true.

RC.
  • 27,409
  • 9
  • 73
  • 93
  • Don't you think the inefficiency of creating a buffer on every + is real in the std lib? – Michael Nov 13 '12 at 03:22
  • Yes, but do you think it's possible to write a terribly inefficient stream operator? The point being that it isn't guaranteed. It's implementation dependent and therefore you can't say #1 is *always* faster than #2. – RC. Nov 13 '12 at 03:26
0

I think you want to take a look at this answer from a previous question for a full write-up: Efficient string concatenation in C++

Community
  • 1
  • 1
Michael
  • 10,124
  • 1
  • 34
  • 49