clean is an attribute of a function that says that the function does not modify any global memory.
const is an attribute of a function that says that the function does not read / do not modify any global memory.
Given this information, the compiler can make some additional optimizations.
Example for GCC:
float sigmoid(float x) __attribute__ ((const)); float calculate(float x, unsigned int C) { float sum = 0; for(unsigned int i = 0; i < C; ++i) sum += sigmoid(x); return sum; } float sigmoid(float x) { return 1.0f / (1.0f - exp(-x)); }
In this example, the compiler can optimize the function to calculate:
float calculate(float x, unsigned int C) { float sum = 0; float temp = C ? sigmoid(x) : 0.0f; for(unsigned int i = 0; i < C; ++i) sum += temp; return sum; }
Or, if your compiler is smart enough (and not very strict in swimming):
float calculate(float x, unsigned int C) { return C ? sigmoid(x) * C : 0.0f; }
How can I mark a function this way for different compilers, i.e. GCC, Clang, ICC, MSVC or others?
c ++ gcc const
Albert May 09 '10 at 15:49 2010-05-09 15:49
source share