[fpc-pascal] If vs case or something else?
Stefan V. Pantazi
svpantazi at gmail.com
Thu Jul 6 22:41:25 CEST 2017
That is quite the case statement you have there. You may benefit greatly
from re-analyzing and refactoring your programs by splitting them in
multiple individual units, if possible. That should be perfectly
possible if Dosomething(); DoSomethingelse(); keep repeating across your
70+ switch cases.
Anyway, here you go, you can try something like the attached.
Good luck!
Stefan
On 07/06/2017 12:33 PM, James Richters wrote:
> I'm trying to optimize a console application ported over from Turbo Pascal with a huge set of if then else statements like this:
>
> Var
> TXT : String;
> If TXT = 'Thing1' then
> Begin
> Dosomething();
> DoSomethingelse();
> End
> else
> If TXT = 'AnotherThing' then
> Begin
> Dosomethinganotherway();
> DoSomethingelsetoo();
> End
> Else
> If TXT = 'Thing3' then
> Begin
> Dosomethingathirdtime();
> DoSomethingelse3();
> End
> else .... etc.. over 70 times... etc
>
> It works but it's hard to follow and not so fast. It has to run through this for every line of a file to see what's in the file and do the right things with it.
> So I'm thinking of undertaking a huge re-organization of it and I'm wondering what the best way would be to do it.
>
> I can use case statement with strings apparently now, but the thing is, I create this file myself in an earlier stage, so I do not need to create the file with strings at all, I could use some codes and do something like this:
>
> Const
> Thing1 = 1
> Another_Thing = 2
> Thing3 = 3
> Var
> Codenum: Word;
>
> Case Codenum of
> Thing1: Begin
> DoSomethng()
> DoSomethingElse()
> End;
> Another_Thing: Begin
> ... etc ...
>
>
> I'm guessing that would be faster as I wouldn't have to keep evaluating all the strings for every condition. As I was searching for the best way to do this, I came across something about making an Array of Procedures, or something to that effect. Does anyone know how to do that, and are there performance advantages to doing that? It's going to take a bit of work to straighten all this out so it would be best if I just did it the best way instead of doing it one way then finding out it could have been better another way. Any other ideas on how to handle this?
>
> Any advice on this is greatly appreciated
>
> James
>
>
>
>
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
--
_______________________________________________________
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fast_switch.pas
Type: text/x-pascal
Size: 630 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170706/ccef91e1/attachment.pas>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fast_switch_dosomething_unit.pas
Type: text/x-pascal
Size: 768 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170706/ccef91e1/attachment-0001.pas>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fast_switch_handler_unit.pas
Type: text/x-pascal
Size: 690 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170706/ccef91e1/attachment-0002.pas>
More information about the fpc-pascal
mailing list