-1

This is a quite generic question but I haven't found clear and specific answers for C++.

I have a class with numerous methods, which may have a large number of instances implemented simultaneaously with calls to a few methods (<5) which are time-critical i.e. for real-time simulation, others methods being not time-critical. Would it be more time-efficient to declare the non-critical methods as static members, or even as non-class functions (as far as relevant/possible).

Now what about memory cost of dynamic methods vs. static methods or non-class methods? Is there a risk of memory shortage with a very large number of instances in either option?

If I missed a reference answering precisely this question, please forgive me or just give me a hint how to proceed, thanks in advance!

TGVF
  • 1
  • 1
  • 1
  • Except "Is there a risk of memory shortage with a very large number of instances in either option?", that question doesn't make sense to me. – Cheers and hth. - Alf Aug 03 '18 at 06:07
  • Instead of attempting to describe some code, please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to *show* us. Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 03 '18 at 06:08
  • Methods (static, non-class) won't affect memory consumption. Its the `sizeof(a instance) + [all the dynamic memory allocated inside instance]` will actually affect the total memory consumption of program. In case if you declare a method as non-class then how are you planning to pass the required data which is present inside class to these non-class methods? – sameerkn Aug 03 '18 at 06:18
  • Switching to non-class I would need to change method signature, adding an instance reference to access its members. I need to try and see how it compiles and performs. Concerning memory consumption, I guess an instance must keep some dedicated space for its own members, including attributes, memory allocation for pointers, stacked adresses, exceptions branching, etc, whilst with a static or non-class method this memory space would be allocated only once and for all. I will test and measure this point with sizeof(type). – TGVF Aug 03 '18 at 08:46

1 Answers1

0

The reason you won't find specific answers is that trying to predict how your optimizer is going to do can be difficult.

I recently dug way deeper than I normally do into the code when trying to get an answer to this question where a supposedly totally harmless change had a small but measurable effect of performance.

If you read the outcome of the deep-dive I did on the code, optimization is a bit like the butterfly principle. A tiny tiny change can have "ripple-on" effects that cause a much larger effect than you'd expect at first glance. Sure, it was only around 3% in that case, but in time critical code, that can matter. I know, I've been there. ;)

Hence @Cheers and hth - Alf is 100% correct. The ONLY way you can know for absolutely sure is to try all possible combinations and to benchmark them.

dgnuff
  • 3,195
  • 2
  • 18
  • 32