Re: Efficiently passing strings?
From: Josh Sebastian (usenet_at_inglorion.com)
Date: 02/22/04
- Next message: Leor Zolman: "Re: Efficiently passing strings?"
- Previous message: Leor Zolman: "Re: minor confusion"
- In reply to: Bob Smith: "Efficiently passing strings?"
- Next in thread: Bob Smith: "Re: Efficiently passing strings?"
- Reply: Bob Smith: "Re: Efficiently passing strings?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Leor Zolman: "Re: Efficiently passing strings?"
- Previous message: Leor Zolman: "Re: minor confusion"
- In reply to: Bob Smith: "Efficiently passing strings?"
- Next in thread: Bob Smith: "Re: Efficiently passing strings?"
- Reply: Bob Smith: "Re: Efficiently passing strings?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|