Ada 95 Quality and Style Guide Chapter 10

Chapter 10: Improving Performance - TOC - 10.5 ALGORITHMS

10.5.3 Case Statement Versus elsif

guideline

  • Use the case statement when measured performance indicates.

  • example

       subtype Small_Int is Integer range 1 .. 5;
       Switch : Small_Int;
       ...
       -- Case statement
       case Switch is
          when 1 => ...
          when 2 => ...
          when 3 => ...
          when 4 => ...
          when 5 => ...
       end case;
    
       -- "elsif construct"
       if Switch = 1 then
          ...
       elsif Switch = 2 then
          ...
       elsif Switch = 3 then
          ...
       elsif Switch = 4 then
          ...
       elsif Switch = 5 then
          ...
       end if;
    
    

    rationale

    Determine the impact of using case statements versus the elsif construct. If the case statement is implemented using a small jump table, then it may be significantly more efficient than the if .. then .. elsif construct.

    See also Guideline 8.4.6 for a discussion of the table-driven programming alternative.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography