Reputation: 11753
I am just curious the effect of functions inside a class. Let me make my question clear with the following example. Suppose we have two class ClassA
and ClassB
, and they have the same number of property variables. The only difference between them is that ClassA
has more functions than ClassB
:
class ClassA
{
public:
int a;
float b;
double c;
void fun1();
void fun2();
bool fun3();
.....
double fun200();
};
class ClassB
{
public:
int a;
float b;
double c;
void fun1();
};
As you can see there are more functions inside ClassA
than ClassB
. Also, we assume that fun2()... fun200()
are independent of fun1()
.
Now, for example, the user wants to use the functionality of fun1
, which can be realized by ClassA
and ClassB
. Then, my question is what's the effect of having many functions inside ClassA
. Will it hinder its performance? Thanks.
Upvotes: 1
Views: 113
Reputation: 50657
Member functions, if they're not virtual, are same as free functions. There is no overhead in its invocation. So you should expect no much difference between the two. Maybe some burden in the compilation time for the class with more functions.
ps: If this is not the bottleneck, you should focus more on optimization of other parts. Premature optimization is the root of all evil. Or probably, redesign this class to make it small as @Wilbert said.
You may also want to check out C++: Performance impact of BIG classes.
Upvotes: 3
Reputation: 37
Code should be implemented to fulfill a necessity, but in the same time to respect a clean design. The performance should not be the issue if you actually need 200 functions and it is best practice that you avoid having so many functions just for the idea of "maybe I'll use them sometime".
Upvotes: 1
Reputation: 7399
Yes, it will hinder performance.
Not the performance of the code that is executed, but the performance of the developer who has to work with this code. Try to keep classes small, easy to understand, and responsible for a single thing only.
Upvotes: 7