Re: Efficiently passing strings?

From: Josh Sebastian (usenet_at_inglorion.com)
Date: 02/22/04


Date: Sat, 21 Feb 2004 20:41:53 -0500

On 21 Feb 2004 17:11:01 -0800, Bob Smith <bobsmith327@hotmail.com> wrote:

> I have a class with a std::string data member. In that class, I have
> a constructor that takes a value for the std::string data member. I
> want to make sure I'm implementing this the most efficient way
> possible. Here's some code to show you what I mean:
>
> #include <iostream>
> #include <string>
> using namespace std;
>
> class Player
> {
> public:
> // --Uncomment on of the following lines--
> //Player(const string& name): m_Name(name) {}
> //Player(const char name[]): m_Name(name) {}
> //Player(const char* name): m_Name(name) {}
> const string& GetName() const {return m_Name;}
> private:
> string m_Name;
> };
>
> int main()
> {
> Player p1("Dirk");
> cout << "Player one's name is " << p1.GetName() << endl;
> }
>
> All three constructors work, but is any more efficient that the other?
> I'm concerned that by passing a string literal to the constructor:
>
> Player(const string& name): m_Name(name) {}
>
> I'm instantiating the same string object twice (once for the parameter
> and once for the data member).
>
> Any thoughts? Thanks.

The second and third versions are actually the same. If you hadn't inlined
the function, there would have been a duplicate definition error. The
first is probably fine after optimization. It all depends on what your
compiler actually does. Compile it and check the code generated by the
compiler; there's really no other way to tell.

Now, as for the GetName() function, it should probably return a
std::string by value rather than a reference. Why? Well, the return will
probably be optimized away anyway (check to make sure, but this
optimization is so common it's explicitly mentioned in the standard), and
using a reference (even a const one) exposes some of your implementation.
If you figure out a much better way to store the string later, you might
not be able to implement it without affecting code that depends on the
return being a reference.

I have a question for you: how much of a difference does it actually make
in your application? Always do it the easiest way first, then profile your
code, and finally fix the parts that actually need fixing.

-- 
Josh


Relevant Pages

  • Re: another very basic question :-)
    ... You would get a better answer than I could give you by asking in a newsgroup for the appropriate compiler or operating system. ... It takes as arguments a list of characters, and a string, and it adds 1 to any character in the string which matches one of the characters in the list. ... there's no reasonable thing for the function to do when the blanklist and the string overlap in any way. ... This is a very simple optimization, and under other circumstances, virtually any modern compiler would perform optimizations like this one for you, even at the lowest optimization levels. ...
    (comp.lang.c)
  • Re: Multiple Inheritance with Interfaces
    ... I cast one to the Interface it compiled, ... it compiles ok only because the java compiler does not ... It is very hard for a compiler to determine what to do in this case, because a carefully timed thread could plant a String into mumble between line 1 and line 2, allowing line 2 to execute without error. ... but with reference expression treated as being capable of referencing ...
    (comp.lang.java.programmer)
  • Re: Numeric value problems
    ... A standard peephole algebraic-simplification optimization, ... any general text on compiler design. ... I've had to build compilers for toy languages and in college (many ... the string concatenation will happen first, then the string "bar * 1" ...
    (comp.lang.javascript)
  • Re: Object reference not set to an instance of an object
    ... TObject" you're declaring a reference to an instance of TObject. ... The compiler can't know what ... The Y TObject variable is just a reference to an object. ... In the Win32 personality of Delphi, that new string is actually a nil ...
    (alt.comp.lang.borland-delphi)
  • Re: comunication between JScript and C#
    ... public function Eval(expr: String): String ... If you have a metadata reference, ... JScript.NET compiler knows about MyNS and you can directly do: ... is there another way to use the jscript function ...
    (microsoft.public.dotnet.languages.csharp)