PreviousNext
Modification instead of storing the value
Help > Appendix > Code Optimization > Modification instead of storing the value

FLASH-                        RAM                Speed+

 

In special cases, it is better to modify the value of the variable than to assign it as the compiler optimizes to the shorter code. The compiler just increments the value in the example below.

 

#define      STATE_A      0

#define      STATE_B      1

#define      STATE_C      2

 

  uns8 state;

  if ( condition1 )

    state = STATE_A;

  else

    if ( condition2 )

       state = STATE_B;

    else

       state = STATE_C;

#define      STATE_A      0

#define      STATE_B      1

#define      STATE_C      2

 

  uns8 state = STATE_A;

  if ( !condition1 )

  {

    state += STATE_B - STATE_A;   // ++

    if ( condition2 )

      state += STATE_C - STATE_B; // ++

  }