Reputation: 493
I have many overloaded functions in a class. In this case, should I declare the int32_t data as a member variable of the class, so I am not declaring it over and over in each function? The Fill function is always setting a value through reference, so I don't think I should need to declare it every time in every function.
There is about 20 more of these functions not listed here:
void TransmitInfo(TypeA &dp, Id &tc)
{
//do lots of other work here
int32_t data;
while (dp.Fill(data)) //Fill accepts a reference variable, "data" gets populated
{
Transmit(tc, data);
}
}
void TransmitInfo(TypeB &dp, Id &tc)
{
//do lots of other work here
int32_t data;
while (dp.Fill(data))
{
Transmit(tc, data);
}
}
void TransmitInfo(TypeC &dp, Id &tc)
{
//do lots of other work here
int32_t data;
while (dp.Fill(data))
{
Transmit(tc, data);
}
}
Upvotes: 3
Views: 78
Reputation: 40645
Scope is not the only thing to consider when choosing where to declare a variable. Just as important are the lifetime of the variable and when it is created.
When you declare a variable inside a function, it is created whenever that function is called, several times if need be (recursion!). And it's destroyed when that function exits. These creations/destructions are noops for the CPU in the case of simple types as int32_t
.
When you declare it inside the class, you get only one copy of the variable per object you create. If one of your function calls another (or itself), they will both use the same variable. You also increase the size of your objects; your variable will consume memory even when it's not used.
So, the bottom line is: Use the different kinds of variables for the purposes they were designed for.
static
function variable.static
class variable.Anything else leads to chaos.
Upvotes: 5
Reputation: 1473
You should refrain from using a member variable for any type of temporary data. The reason for this is that it guarantees that your code is not thread safe, and in this day-and-age of parallel computing, that is a major disadvantage. The cost of allocating an int32_t
is extremely small as to be negligible so thus it is often better to allocate inside the function to maintain thread safety. Before a single int
allocation becomes noticeable you will have to allocate it well over a million times, and even then the total loss will be in microseconds.
If your experiencing such difficulty with optimization that you have to resort to such a high degree of micro-optimization then you likely should try and rework your algorithm to create a better scaling as opposed to spending massive amounts of time optimizing something that is not a choke point. (You would also be better off using a good concurrent algorithm, as opposed to shaving picoseconds off of a serial algorithm.)
Upvotes: 2
Reputation: 234785
Absolutely do not do this. If it's only a temporary for the life of a function then keep it local.
Else you'll cause more problems than you solve; e.g. Multithreading and serialisation.
Leave such micro-optimisations to the compiler.
Upvotes: 2