Max Function

A task frequently undertaken in programming is determining the highest value.  One approach is to compare values using IF statements, as illustrated in this simple example.  This program prints the highest value of the three variables, which would be 19.

  var i,j,k: integer;
  begin
    i:=7; j:=2; k:=19;
    if (i>j) and (i>k) then writeln(i)
    else if (j>i) and (j>k) then writeln(j)
    else writeln(k);
    CompilerStats;
  end;

The CompilerStats statement reports the resources used by this program.  The compiled program used 5 labels, 6 constants, 27 stack items, and 54 instructions (tokens).

  ARRAY   MAX  USED
  Label   500     5
  Const  2000     6
  Stack  6000    27
  Token 13000    54

It is not the purpose of this tutorial to dig into the inner workings of the ESPL compiler.  Rather, I am interested in the efficiency gained by a 2nd approach.  Instead of finding the highest value using IF statements, the MAX function can be used.  The MAX function returns the highest value from among its parameters.  MAX may have up to 100 parameters.  The task of the 1st example can be coded more efficiently this way.

  var i,j,k: integer;
  begin
    i:=7; j:=2; k:=19;
    writeln(max(i,j,k));
    CompilerStats;
  end;

This time the program used 1 label, 5 constants, 16 stack items, and 32 instructions.  The statistic we are most interested in is the number of instructions because that affects execution speed. 

  ARRAY   MAX  USED
  Label   500     1
  Const  2000     5
  Stack  6000    16
  Token 13000    32