Reputation: 17553
Is there a significant performance hit if I keep adding member functions to a class? When I use the class, I may only use a couple of the member functions together at once so I could in theory split the single class into a number of smaller classes with fewer member functions. Do I take a big performance hit by cramming a lot of functions into the same class?
Upvotes: 2
Views: 580
Reputation: 206508
No, it doesn't matter.
But presence of lot of public` api indicates that you should make sure you are following the Single responsibility principle if you are trying to have a good design.
There should not be more than one reason for a class to change.
If you design adheres to that, then its all good and cramming a lot of functions to the class is not going to do any bad in terms of performance.
Some people might argue about performance hit if those functions are virtual
however as long the purpose of those functions is to be overidden in the derived class then you should make them virtual
, that is unless those functions are not being made virtual
just for the sake of flexibility but on basis of a well thought design then go ahead and make them virtual
.
Performance hits shouldn't be a concern its the price you pay for a functionality you want to have just that.
Also, Only profiling can actually give you accurate indications about performance bottle necks without it, what you get is speculations or guesses on the basis of experience which always might not be truly indicative.
Upvotes: 4
Reputation: 3307
The performance hit comes not from having lots of functions but having a deep hierarchy of functions calls to do a given task. Every function call results in
Pushing the parameters on the stack.
Execute the function.
So e.g. If in sequence of execution if you end up calling 10functions you wind up stack 10 times and when the function(/s) is finished the stack has to be unwound.
Secondly there are gcc optimizations to reduce the cost of a jump to your function I the .Text
e.g. By using an attribute
called hot
that improves the locality of such functions so that access of such functions is faster.
You always have to think about a function execution as *in the context of the thread executing it * so that you can identify various bottle necks and optimize them.
Upvotes: 2
Reputation: 59811
No, there shouldn't be and there wouldn't be any difference in binary size if you break up the class into smaller pieces. If you want to reduce binary size to only the function you call you can make your class a template.
Upvotes: 1