[comp.lang.pascal] CASE vs. IF

aeba-im-o-e2@berlin-emh1.army.mil ( IM EMAIL ASST SYS ADMIN) (06/05/91)

>> so i can't use case for strings, eh?  what are my alternatives?  a list of
>> if-thens?  (tp5.0)
>> 
>
>A list of if-then's in not so bad if you structure them correctly.  After all
>isn't that what case is all about?  As an alternative you may want to convert

Is there any run-time difference between using case statements or the equivalentof if-thens?

I am wondering because I use UNIX a bit and have a reference that says 
cases tend to run faster than if-thens in UNIX.  Might the same hold true for
Pascal? Or the opposite? Or no difference?

Ken Gibson
_______________________________________________________________________
|  SGT Kendrick J. Gibson          aeba-im-o-e2@berlin-emh1.army.mil  |
|  Asst. System Administrator; U.S. Army Berlin Electronic-mail Host  |
{$I DISCLAIM.STD}
Writeln (FamousQuote);

phys169@csc.canterbury.ac.nz (06/07/91)

In article <27118@adm.brl.mil>, aeba-im-o-e2@berlin-emh1.army.mil ( IM EMAIL ASST SYS ADMIN) writes:
> Is there any run-time difference between using case statements or the 
> equivalent if-thens? ...cases tend to run faster than if-thens...

The answer depends on the compiler and often on the number of cases. The Turbo
Pascal compiler generates code to try options, in the order given in the case
statement. Other compilers might generate a table look-up (which takes a bit of
time & memory overhead, but the increase in time as you add to the case
statement can be nil). One example I remember was that if you have less than 5
alternatives in Burroughs Algol, it was more efficient to use if-then-elses. As
I said, it depends on the compiler, etc.

Mark Aitchison, Physics, University of Canterbury, New Zealand.

ins845b@monu4.cc.monash.edu.au (mr k.l. lentin) (06/08/91)

In article <1991Jun7.114712.985@csc.canterbury.ac.nz> phys169@csc.canterbury.ac.nz writes:
>In article <27118@adm.brl.mil>, aeba-im-o-e2@berlin-emh1.army.mil ( IM EMAIL ASST SYS ADMIN) writes:
>> Is there any run-time difference between using case statements or the 
>> equivalent if-thens? ...cases tend to run faster than if-thens...
>
>The answer depends on the compiler and often on the number of cases. The Turbo
>Pascal compiler generates code to try options, in the order given in the case
>statement. Other compilers might generate a table look-up (which takes a bit of
>time & memory overhead, but the increase in time as you add to the case
>statement can be nil). One example I remember was that if you have less than 5
>alternatives in Burroughs Algol, it was more efficient to use if-then-elses. As
>I said, it depends on the compiler, etc.

If you use cases with sets then they will run quite slowly. Turbo's set
manipulation is quite slow. i.e.
case i of
[1..5] : etc

A great way of finding out which is faster when you have two different ways of
doing things is quiite simple. Call gettime and print it out. Then execute
the desired function or whatever about 20000 or 50000 times then do the same
thing for the alternative. This removes and affects getting of the time might
have or any rounding effects on short pieces of code (i.e. 1.1 secs vs 1.2
secs can come out the same.) it also gives you nice big numbers to compare.
i once rewrote a lot of my string handling routines and used the method to
shave tiny amounts of time off (so small nobody would ever notice the
difference.) and it saves trying to come to grips with thr profiler!


--
-----------------------------------------+----------------------------------
|/     (ins845b@monu4.cc.monash.edu.au)  | This space for rent.
|\evin (ins845b@monu3.cc.monash.edu.au)  | All reasonable offers accepted
-----------------------------------------+---------------------------------