PreviousNext
Better to return from than after the loop
Help > Appendix > Code Optimization > Better to return from than after the loop

FLASH-                        RAM                Speed+

 

It is more efficient to return from the function in the middle of the loop than to exit the loop then return so internal “goto to the return” can be avoided.

 

void function ()

{

  uns8 loop;

  for ( loop = 10; --loop != 0; )

  {

       nop2();

       nop2();

  }

}

void function ()

{

  uns8 loop;

  for ( loop = 10;; )

  {

       if ( --loop == 0 )

         return;

 

       nop2();

       nop2();

  }

}

 

The same applies to the return from the function itself.

 

void Function()

{

  if ( condition1 )

  {

       nop();

       if ( condition2 )

       {

         nop();

       }

  }

}

void Function()

{

  if ( !condition1 )

       return;

 

  nop();

 

  if ( !condition2 )

       return;

 

  nop();

}