Quick question about string concatentation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Quick question about string concatentation

  1. #1
    Join Date
    Feb 2013
    Posts
    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;

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Quick question about string concatentation

    What is String?
    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

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,067

    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;

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Quick question about string concatentation

    Quote Originally Posted by surfy24 View Post
    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.
    also the Big O notation.
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center