For numerical constants, I see two options:
Option one: use static readonly (calculated once at startup):
class MyCalc { private static readonly double spi = Math.Sin(Math.PI); private static readonly double pi = Math.Sin(Math.PI); public void Execute() {
Option two: do the calculations with your pocket calculator and copy these constants:
class MyCalc {
I'm not sure if the compiler can fully optimize the option one in the calculation, but it should be the most readable and supported way.
If you are looking for as much compilation time as possible, things get more complicated. In C ++, you have templates. I find them cumbersome to write, but people get amazing things done with it. It seems easier to compile time functions with , but I have not tried them yet. D have CTFE , which is really powerful. But D is a niche, and I would not write serious code in it. I do not know other languages ββwith a significant explicit assessment of precompilation, but I am sure that there are some.
Compilers are no longer smart today. Most likely, the compiler can see the ability to integrate the optimization of a function call without a hint. With DotNet 4.5, we have an AggressiveInlining -attribute so that we can force the compiler in the right direction. C / C ++ have something similar, and there were problems. General recommendations on my part are to avoid inline until you know exactly what you are doing.
If you really do not want to go this route with C #, the best option in my eyes would be to write your functionality in C ++ using the specified functions, write an easy-to-use C-interface and call it PInvoke . But do yourself a favor and try on if it is really worth it. Never forget two optimization rules:
source share