Board index » delphi » case statement efficiency

case statement efficiency

I have been writing this interpereter in TP using tply ( a free
lex, yacc pair for TP).  The project is going smoothly except
for efficiecny.  The main loop for the interp part is a huge
case statement ala

case statementtype of

expression : ...;

writeln : ...;

ifthenelse : ...;

else : {oops I forgot to write code for that one };

end;

The problem is that the case statement seems to be syntactic sugar
for a long string of if then else if .. statements.  This annoys me.
I would really like to implement a jump table and I do not (cant?) want
to do it in assembler.  I can not create an array of labels to
implement the more efficient jump table so I am kind of stuck.

I thought about setting up each case as a procedure and having
an array of procedures and calling them thusly

procarray[statementtyp](par1, par2, par3...);

but this seems annoying due to
1) I have to make all of the procedure take the same parameter list.
2) It is harder to simulate a variable parameter list structure
    this way
3) I do not know that the overhead of setting up a procedure call
     and calling that procedure will result in an efficiency gain.

I do know that on average 1/2 of the if/then/else statements of the
case statement will fail resulting in the program blowing the
processor prefetch queue multiple times per loop.  The procedure
would blow the queue once and once only.  This to me is where
I might see some efficiency gain.

Any comments are welcome.  Especially the variety of comment "Oh thats
easy...", or, Just look in swag.

Ephram Cohen
eph...@ear.berkeley.edu

 

Re:case statement efficiency


Ephram,

you can do this using procedure-variables. Enjoy the following
example.

Joe Merten

    Uses Crt;

    Type TCommand=(cmdFirst,cmdSecond,cmdThird);

    Type TCommandParams=Record
            nParam1: Word;
            nParam2: Word;
            nParam3: Word;
            end;

    {Typedeclaration of the JumpTable-Array}
    Type TProcArr=Array[TCommand] of
        Procedure(var aParamRec: TCommandParams);

    Procedure PrepareFirst(var aParamRec: TCommandParams); Far;
        Forward;
    Procedure PrepareSecond(var aParamRec: TCommandParams); Far;
        Forward;
    Procedure PrepareThird(var aParamRec: TCommandParams); Far;
        Forward;

    {Setting up the JumpTable. }
    Const aProcArr: TProcArr=(
        PrepareFirst,PrepareSecond,PrepareThird);

    var c: Char;

    Procedure PrepareFirst(var aParamRec: TCommandParams);
      begin
      Writeln('Preparing the 1st Command');
      end;

    Procedure PrepareSecond(var aParamRec: TCommandParams);
      begin
      Writeln('Preparing the 2nd Command');
      end;

    Procedure PrepareThird(var aParamRec: TCommandParams);
      begin
      Writeln('Preparing the 3rd Command');
      end;

    Procedure PrepareCommand(eCmd: TCommand);
      var aParas: TCommandParams;
      begin
      {Calling the required Procedure}
      aProcArr[eCmd](aParas);
      end;

    begin
    repeat
      c:=ReadKey;
      if (c>='1') and (c<='3') then
        PrepareCommand(TCommand(Ord(c)-Ord('1')));
    until c=#27;
    end.

Quote
Ephram Cohen <eph...@ear.berkeley.edu> wrote:
>I have been writing this interpereter in TP using tply ( a free
>lex, yacc pair for TP).  The project is going smoothly except
>for efficiecny.  The main loop for the interp part is a huge
>case statement ala
>case statementtype of
>expression : ...;
>writeln : ...;
>ifthenelse : ...;
>else : {oops I forgot to write code for that one };
>end;
>The problem is that the case statement seems to be syntactic sugar
>for a long string of if then else if .. statements.  This annoys me.
>I would really like to implement a jump table and I do not (cant?) want
>to do it in assembler.  I can not create an array of labels to
>implement the more efficient jump table so I am kind of stuck.
>I thought about setting up each case as a procedure and having
>an array of procedures and calling them thusly
>procarray[statementtyp](par1, par2, par3...);
>but this seems annoying due to
>1) I have to make all of the procedure take the same parameter list.
>2) It is harder to simulate a variable parameter list structure
>    this way
>3) I do not know that the overhead of setting up a procedure call
>     and calling that procedure will result in an efficiency gain.
>I do know that on average 1/2 of the if/then/else statements of the
>case statement will fail resulting in the program blowing the
>processor prefetch queue multiple times per loop.  The procedure
>would blow the queue once and once only.  This to me is where
>I might see some efficiency gain.
>Any comments are welcome.  Especially the variety of comment "Oh thats
>easy...", or, Just look in swag.
>Ephram Cohen
>eph...@ear.berkeley.edu

Other Threads