C ++ string pointers
In my previous application, I had an object like this:
class myType
{
public:
int a;
string b;
}
There were many cases in it, scattered everywhere and going up to almost every function.
The app was slow. Profiling says that 95% of the time is consumed by the row distribution function.
I know how to work with the object above, but not how to work with string pointers.
class myType
{
public:
int a;
string* b;
}
They told me to use pointers as above.
- How much faster does it work with a string pointer?
-
What is copied when copying an object?
-
How to use the following class using a pointer:
Accessing a string value
Change the string value without changing the object in the object (copy?)
Common things that change if I use string pointers?
a source to share
It will probably be slower - you still need to create and copy rows, but now you have the overhead of dynamically placing on top. My guess is that you are copying your objects too much - whenever you call a function, your myType objects should be passed as const references where possible, and not by value:
void f( const myType & mt ) {
// stuff
}
If you really need to change mt, you would use a non-const reference - it's also cheaper than passing in a value and returning a new value with the changed fields.
I think using a pointer like this is a bad idea. Instead, look at how yours is used myType
. Specifically, instead of doing this:
void foo(myType a)
{
// ...
}
Consider this:
void foo(myType const &a)
{
// ...
}
In the first case, you need to create a copy of myType in order to go to the foo () function, in the second case, a copy is not required, since a reference is passed instead (it is marked as const
, so you can be sure that you are foo()
not trying to modify it, giving you (almost ) the same behavior as the first method).
There are probably other things you could change, but my guess is that this will make you the best for your dollar (and this is a pretty mechanical change, so hopefully not too much of a problem)
a source to share
To add to the other answers, you should also make sure that any member functions of this class that pass a string are passed by const reference. For example, let's say the definition of your class constructor looks like this:
myType::myType(int a, string b)
Use this instead:
myType::myType(int a, const string& b)
So, in general, go through all your functional parameters throughout the project and change string
to const string&
and myType
to const myType&
. That alone should fix most of your performance issues.
Note. About dynamically allocating a string and passing it as a pointer. This is not a good idea. As much as it lightens the performance burden a bit, you will be extremely vulnerable to memory leaks, making debugging a nightmare (in addition to being much more messy destructive to your performance than being slow). As a general rule, I strongly dislike bare pointers. There is almost always a better, safer alternative.
a source to share