-
February 25th, 2013, 03:14 AM
#1
Quick question about string concatentation
What is the efficiency of the two assignments (line 1 and 2), i.e. (function calls, number of copies made, etc), also the Big O notation. I know there are function calls for retrieving the size of each string in order to produce a new buffer for the concatenated string...any difference between line 1 and 2 in terms of efficiency?
String s("Hello");
String t("There");
1. s = s + t;
2. s += t;
-
February 25th, 2013, 04:51 AM
#2
Re: Quick question about string concatentation
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
February 25th, 2013, 05:21 AM
#3
Re: Quick question about string concatentation
No answer possible with the given information.
It depends on how the += operator is implemented, how the binary + is implemented and whether String has an assignment operator with move semantics.
that said.
the += operator has more potential for being solved more efficiently than the other. Whether is actually is, is another matter;
-
February 25th, 2013, 05:31 AM
#4
Re: Quick question about string concatentation
Originally Posted by surfy24
What is the efficiency of the two assignments (line 1 and 2), i.e. (function calls, number of copies made, etc),
First, where is the implementation of String? How can we answer a question if we don't see what "String" is and its implementation? Maybe the implementation is awful for operator += and great for operator + (or vice-versa).
Second, even if we saw the implementation, asking "how many copies are made" cannot be answered, because there is no answer. Copying objects and how many copies are made is solely up to the compiler and compiler settings when it comes to C++. The "how many copies are made" is a trick question.
There is no "Big O" for this, unless there is a specific algorithm being implemented. You can't judge how fast or how slow high-level C++ code is unless it is obvious (i.e. a nested loop that is done "N" times for each loop is obviously N^2, a binary tree search is obviously logarithmic, etc.).
Too many times, we have newbie C++ or even veteran C coders (who are not experienced C++ coders) trying to eyeball high-level C++ code and determine what is fast or slow. What winds up happening is that their predictions are usually wrong.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|