Recent

Author Topic: Lazarus / delphi loop efficiency: continue or begin..end?  (Read 13021 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Lazarus / delphi loop efficiency: continue or begin..end?
« Reply #30 on: March 16, 2018, 05:58:40 am »
I just wanted to show the "nesting" problem.

Let's see some other case.

Code: Pascal  [Select][+][-]
  1. for i := 0 to N do beign
  2.     if cond1 then begin
  3.          State := 1;
  4.          Var1 := afunction(state);
  5.     end
  6.     else begin
  7.         //  do something;
  8.  
  9.         if cond2 then begin
  10.             State := 2;
  11.             Var2 := afunction(state);
  12.         end  
  13.         else begin
  14.              //  Do others
  15.  
  16.             if cond3 then begin
  17.                  State := 3;
  18.                  Var1 := anotherfunction(State);
  19.                  Var2 := afunction(State3);
  20.             end
  21.             else begin
  22.                   // Do more things
  23.             end
  24.        end
  25.    end
  26. end;
  27.  

We can write this as:

Code: Pascal  [Select][+][-]
  1. for i := 0 to N do beign
  2.      if cond1 then begin
  3.          State := 1;
  4.          Var1 := afunction(state);
  5.          Continue;
  6.     end ;
  7.  
  8.      //  do something;
  9.  
  10.      if cond2 then begin
  11.          State := 2;
  12.          Var2 := afunction(state);
  13.          Continue;
  14.      end ;
  15.  
  16.      //  Do others
  17.  
  18.      if cond3 then begin
  19.           State := 3;
  20.           Var1 := anotherfunction(State);
  21.           Var2 := afunction(State);
  22.           Continue;
  23.      end;
  24.  
  25.      // Do more things
  26. end;
  27.  

And think of cases where are rather lot of codes in the commented part of "do something", "do others", etc. The same with Exit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Lazarus / delphi loop efficiency: continue or begin..end?
« Reply #31 on: March 16, 2018, 09:22:58 am »
Code: Pascal  [Select][+][-]
  1. for i := 0 to N do
  2.    case condition of
  3.      cond1:begin
  4.              State := 1;
  5.              Var1 := afunction(state);
  6.            end;
  7.      cond2:begin
  8.              State := 2;
  9.              Var2 := afunction(state);        
  10.            end ;
  11.      cond3:begin
  12.              State := 3;
  13.              Var1 := anotherfunction(State);
  14.              Var2 := afunction(State);
  15.            end;
  16.     end;
  17.  

Looks much nicer and is faster
Specialize a type, not a var.

 

TinyPortal © 2005-2018