So I have a std::string that is always used every time the function gets called.
void doSomething()
{
...
std::string temp_str;
... // bunch of codes which reads and writes to temp_str
}
Now I'm guessing, isn't it faster to change std::string temp_str
to static std::string temp_str
?
As mentioned here, I know that if the variable is allocated in the stack, what it does is just subtraction of the ESP, so it doesn't affect the performance much.
However, since the std::string
allocates its buffer in heap, doesn't making it static
make it reuse the memory?
If the variable is automatic, isn't it a waste to new
and delete
memory every time the function gets called?
Well, this is not only about std::string
. My question is:
Is static object in a function faster than automatic if it has a reusable resource (like dynamically allocated memory) as a member?