We are often told things like
If you call a method with a return value that does not change, remove it from the loop.
for example, when writing code like:
for(int i=0; i < Instance.ExpensiveNonChangingMethod(); i++) {
I was wondering if you could somehow tell the compiler that given the same input (and the object instance) you would get the same outputs, so he would know that he could take it out of the loop as part of the optimization process.
Is there anything similar? Perhaps something like code-contracts or spe # ? Or can they already do such things?
Is it possible to assume that the compiler can assume that the method is a pure function itself?
Is there anything I can use in C # that allows the compiler to do a lot of optimization, which otherwise would be? Yes, I know about premature optimization. I ask mainly out of curiosity, but also, if something like the above existed, it would be "free" as soon as the method was noted.
source share