I have read in many threads, that from a performance standpoint, it is good to declare basic variables like
bool etc. as close as possible. It is therefore preferable to declare a large number of variables in the context of
MyClass::method(), that in the
MyClass body. Is it the truth?
But what if I need a lot of variables and have to use them in two separate functions.
// here I use a lot of variables
secondMethod(); // and here I need all those variables
It is therefore preferable (always thinking about performance) to declare all these variables in the body of
MyClass? Or is it better to do something like this:
float var001, var002 ... var100;
// here I make some calculations with all variables
secondMethod(var001, var002 ... var100);
void MyClass::secondMethod(float &v001, float &v002 ... float &v100)
// do some calculations
Of course, this sounds silly when the method gets 100 input parameters. In the book "Clean Code" by Robert C. Martin, I read that the methods should not take more than 3 or 4 input parameters. But I wonder if it has good performance in such not typical algorithms?
I'm working on an audio processor and in the audio process block I have to calculate a lot of things like calculating MID / SIDE samples, multiplying by input gain, output gain, filtering, dynamic analysis , multiply by the gain reduction coefficient and send some of these variables to the graphical wires to display them on certain audio monitors, and many other things. And I have to do this independently for each sample, and there are tens of thousands of samples per second. So is it a good practice to declare as much as possible in the scope, or better in the class body?
Please note that many of my variables must also be
std::atomic, so these are not just basic variables like I said at the start. I don't know how to declare all of this to provide the best performance.
I also have more than one processor block for different audio processes that I can choose in real time in the graphical interface of my application. So is it better to make a big processor block with lots of if (or swich) instructions for choosing the processor? Or better to declare a labmda for which I can assign various processes and then call only this lambda in the main process block? And the problem is that I also have varius algorithms in each of my different processors. So I find myself calling lambdas inside other lambdas.
I am wondering how to handle all these problems to provide the best performance, but also to provide clean code as much as possible.
For any help, thank you in advance.