Using double for intermediate results

I am writing a Simulation program and am wondering if using const double can be used while saving intermediate results. Consider this snippet:

double DoSomeCalculation(const AcModel &model) { (...) const double V = model.GetVelocity(); const double m = model.GetMass(); const double cos_gamma = cos(model.GetFlightPathAngleRad()); (...) return m*V*cos_gamma*Chi_dot; } 

Please note that the sample is shown for illustration only - it may not make much sense on the technical side. The motivation for saving, for example, cos_gamma in a variable, is that this cosine is used many times in other expressions covered by (...), and I feel that the code becomes more readable when using

 cos_gamma 

but not

 cos(model.GetFlightPathAngleRad()) 

in various terms. Now the real question is this: since I expect the cosine to be the same as in the code section, and I actually created the thing only as a placeholder, and for convenience I tend to declare it const. Is there a naked opinion that this is good or bad, or can he bite me at the end? Does the compiler help to use this additional information, or am I really preventing the compiler from performing useful optimizations?

Arne

+4
source share
5 answers

Given your code:

 const double V = model.GetVelocity(); const double m = model.GetMass(); const double cos_gamma = cos(model.GetFlightPathAngleRad()); 

I would probably leave cos_gamma as it is. I would think of changing V and m to the links:

 const double &V = model.GetVelocity(); const double &m = model.GetMass(); 

Thus, you make it clear that these are strictly placeholders. However, this increases the likelihood of problems with longevity - if you use a link, you must be sure that what it refers to has a sufficient lifetime. At least in terms of things, this is probably not going to be a problem. First of all, GetVelocity() and GetMass() will probably return values, not links (in this case, you initialize links with temporary parameters, and the lifetime of the temporary extension extends to the lifetime of the initialized link). Secondly, even if you return the actual link, it apparently belongs to the model member, which (by assumption) will exist throughout the whole question under consideration in any case.

+3
source

I am not sure about the optimization part, but I think it is good to declare it as const . This is because if the code is large, then if someone incorrectly makes cos_gamma = 1 between you, you will get a compiler error instead of runtime surprises.

+6
source

You can certainly help only by calculating the cosine once and using it everywhere. Creating this const result is a great way to ensure that you (or someone else) are not trying to change it somewhere in the future.

A good rule of thumb is to make it correct and readable in the first place. Do not worry about any optimizations that the compiler might or could not do. Only after profiling and discovering that the code snippet is really too slow should you worry about helping the compiler optimize things.

+4
source

I'm sorry that I did not work with a lot of code written!

Everything you can do to make your code more readable can only be good. Optimize only when you need to optimize.

My biggest beef with C ++ is that the default values ​​are not equal to const . Use const generously and keep your legs bulletproof!

+2
source

Does the compiler use this interesting question - as soon as you are at the stage of optimizing a fully working program. Until then, you write the program mainly for people who come later, and should look at the code. The compiler will gladly swallow (objectively) a very unreadable code, without spaces, new lines or sporty indentation. People will not.
The most important question is how readable the code is , and how easy it is to make the error change. . Here const helps tremendously, because it makes the compiler bark at everyone who mistakenly changes everything that shouldn't change. I always do everything const if I really don't want this to be volatile.

0
source

Source: https://habr.com/ru/post/1310846/


All Articles