Hi, Seeing as alternative techniques for exiting a procedure and for ending interation are popular subjects recently, another question about alternatives. Elimination of IF and CASE has some aesthetic appeal. Otherwise, is the array technique ever worth using? OK when most elements in the array are distinct? How fast is array indexing compared to jumping? How bad is the additional procedure call? Thanks for comments, ... Lyall E. ============================== MODULE Cases IN Oberon; IMPORT Texts, Oberon; VAR choice: INTEGER; case: ARRAY 3 OF PROCEDURE; W: Texts.Writer; PROCEDURE Case0(); BEGIN Texts.WriteString(W, "Case 0"); Texts.WriteLn(W) END Case0; PROCEDURE Case1(); BEGIN Texts.WriteString(W, "Case 1"); Texts.WriteLn(W) END Case1; PROCEDURE Case2(); BEGIN Texts.WriteString(W, "Case 2"); Texts.WriteLn(W) END Case2; PROCEDURE Test*(); BEGIN choice := 1; CASE choice OF 0: Texts.WriteString(W, "CASE 0"); Texts.WriteLn(W) |1: Texts.WriteString(W, "CASE 1"); Texts.WriteLn(W) |2: Texts.WriteString(W, "CASE 2"); Texts.WriteLn(W) END; case[0] := Case0; case[1] := Case1; case[2] := Case2; choice := 2; case[choice](); Texts.Append(Oberon.Log, W.buf) END Test; BEGIN Texts.OpenWriter(W) END Cases. Cases.Test -- Message composed and transmitted by software designed to avoid the complication and vulnerability of antivirus software. 123456789 123456789 123456789 123456789 123456789 123456789 123456789 Pender Is., BC: +1 604 670 0140 Washington State: +1 360 639 0202 http://easthope.ca/Peter.html Bcc: peter at easthope. ca
-----Original Message----- From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of peter@easthope.ca Sent: Thursday, 7 March 2019 6:20 AM To: oberon@lists.inf.ethz.ch Subject: [Oberon] Alternatives for cases.
Hi,
Seeing as alternative techniques for exiting a procedure and for ending interation are popular subjects recently, another question about alternatives.
Elimination of IF and CASE has some aesthetic appeal. Otherwise, is the array technique ever worth using? OK when most elements in the array are distinct? How fast is array indexing compared to jumping? How bad is the additional procedure call?
This is one of my test CASEs - I hope it's self-explanatory. Try implementing that using procedures and see what you think. Consider the resulting readability / maintainability / code size as well as speed of execution: IF (ch <= 02FX) OR (ch >= 07BX) THEN INC(others) ELSE CASE ch OF 03AX..040X, 05BX..060X: INC(others) | "a", "e", "i", "o", "u": INC(vowels); INC(letters) | "A", "E", "I", "O", "U": INC(vowels); INC(letters); INC(capitals) | "b".."d", "f".."h", "j".."n", "p".."t", "v".."z": INC(letters) | "B".."D", "F".."H", "J".."N", "P".."T", "V".."Z": INC(letters); INC(capitals) | "0".."9": INC(digits) END END Regards, Chris Chris Burrows CFB Software http://www.astrobe.com
Chris Looking at your code I assume your compiler subtracts minCaseLabel (in your case "0") from "ch", right? Jörg Am 06.03.19, 21:44 schrieb "Oberon im Auftrag von Chris Burrows" <oberon-bounces@lists.inf.ethz.ch im Auftrag von chris@cfbsoftware.com>: > -----Original Message----- > From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of > peter@easthope.ca > Sent: Thursday, 7 March 2019 6:20 AM > To: oberon@lists.inf.ethz.ch > Subject: [Oberon] Alternatives for cases. > > Hi, > > Seeing as alternative techniques for exiting a procedure and for > ending interation are popular subjects recently, another question > about alternatives. > > Elimination of IF and CASE has some aesthetic appeal. > Otherwise, is the array technique ever worth using? > OK when most elements in the array are distinct? > How fast is array indexing compared to jumping? > How bad is the additional procedure call? > This is one of my test CASEs - I hope it's self-explanatory. Try implementing that using procedures and see what you think. Consider the resulting readability / maintainability / code size as well as speed of execution: IF (ch <= 02FX) OR (ch >= 07BX) THEN INC(others) ELSE CASE ch OF 03AX..040X, 05BX..060X: INC(others) | "a", "e", "i", "o", "u": INC(vowels); INC(letters) | "A", "E", "I", "O", "U": INC(vowels); INC(letters); INC(capitals) | "b".."d", "f".."h", "j".."n", "p".."t", "v".."z": INC(letters) | "B".."D", "F".."H", "J".."N", "P".."T", "V".."Z": INC(letters); INC(capitals) | "0".."9": INC(digits) END END Regards, Chris Chris Burrows CFB Software http://www.astrobe.com -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
Yes - That's right. When Oberon-07 numeric CASE only accepted integers it wasn't necessary, but now characters are accepted a CASE statement with a minimum label of "a" would consume almost an extra 400 bytes without this tweak, Chris.
-----Original Message----- From: Jörg [mailto:joerg.straube@iaeth.ch] Sent: Thursday, 7 March 2019 5:43 PM To: chris@cfbsoftware.com; ETH Oberon and related systems Subject: Re: [Oberon] Alternatives for cases.
Chris Looking at your code I assume your compiler subtracts minCaseLabel (in your case "0") from "ch", right? J�rg
-----Original Message----- From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of Chris Burrows Sent: Thursday, 7 March 2019 7:14 AM To: 'ETH Oberon and related systems' Subject: Re: [Oberon] Alternatives for cases.
-----Original Message----- From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of peter@easthope.ca Sent: Thursday, 7 March 2019 6:20 AM To: oberon@lists.inf.ethz.ch Subject: [Oberon] Alternatives for cases.
Hi,
Seeing as alternative techniques for exiting a procedure and for ending interation are popular subjects recently, another question about alternatives.
Elimination of IF and CASE has some aesthetic appeal. Otherwise, is the array technique ever worth using? OK when most elements in the array are distinct? How fast is array indexing compared to jumping? How bad is the additional procedure call?
This is one of my test CASEs - I hope it's self-explanatory. Try implementing that using procedures and see what you think. Consider the resulting readability / maintainability / code size as well as speed of execution:
IF (ch <= 02FX) OR (ch >= 07BX) THEN INC(others) ELSE CASE ch OF 03AX..040X, 05BX..060X: INC(others) | "a", "e", "i", "o", "u": INC(vowels); INC(letters) | "A", "E", "I", "O", "U": INC(vowels); INC(letters); INC(capitals) | "b".."d", "f".."h", "j".."n", "p".."t", "v".."z": INC(letters) | "B".."D", "F".."H", "J".."N", "P".."T", "V".."Z": INC(letters); INC(capitals) | "0".."9": INC(digits) END END
I've just done a test run using an array of Procedure variables with 50000 characters (almost) evenly distributed from CHR(0) to CHR(127). The results show that for this application, procedure variables are not a good idea: Data (bytes) Code (bytes) Time (ms) PROCs 540 1404 65 IF-ELSE 8 864 53 CASE 8 1080 42 Apart from anything else, initialising the array of procedures in what I considered to be an elegant way took a bit of thought. There are many different ways it could be done. Mine looks like this: InitMinMax(0, LEN(Procs) - 1, Others); InitChars("bcdfghjklmnpqrstvwxyz", LowerLetters); InitChars("BCDFGHJKLMNPQRSTVWXYZ", UpperLetters); InitChars("aeiou", LowerVowels); InitChars("AEIOU", UpperVowels); InitChars("0123456789", Digits) where the last parameter in each is the name of the procedure. InitMinMax and InitChars are both just one-line procedures. Regards, Chris Chris Burrows CFB Software http://www.astrobe.com
Lyall your „case[choice]()“ is basically what a „CASE choice OF“ does. So there is no clear runtime benefit of your aporoach vs. CASE. But you have to declare PROCEDUREs that is not needed in case of CASE. A procedure generates quite some overhead during BEGIN and END. So the runtime behaviour of the jump as such is comparable to CASE but the code you jump to takes longer in your approach compared to CASE. br Jörg
Am 06.03.2019 um 20:49 schrieb <peter@easthope.ca> <peter@easthope.ca>:
Hi,
Seeing as alternative techniques for exiting a procedure and for ending interation are popular subjects recently, another question about alternatives.
Elimination of IF and CASE has some aesthetic appeal. Otherwise, is the array technique ever worth using? OK when most elements in the array are distinct? How fast is array indexing compared to jumping? How bad is the additional procedure call?
Thanks for comments, ... Lyall E.
============================== MODULE Cases IN Oberon; IMPORT Texts, Oberon; VAR choice: INTEGER; case: ARRAY 3 OF PROCEDURE; W: Texts.Writer;
PROCEDURE Case0(); BEGIN Texts.WriteString(W, "Case 0"); Texts.WriteLn(W) END Case0;
PROCEDURE Case1(); BEGIN Texts.WriteString(W, "Case 1"); Texts.WriteLn(W) END Case1;
PROCEDURE Case2(); BEGIN Texts.WriteString(W, "Case 2"); Texts.WriteLn(W) END Case2;
PROCEDURE Test*(); BEGIN choice := 1; CASE choice OF 0: Texts.WriteString(W, "CASE 0"); Texts.WriteLn(W) |1: Texts.WriteString(W, "CASE 1"); Texts.WriteLn(W) |2: Texts.WriteString(W, "CASE 2"); Texts.WriteLn(W) END;
case[0] := Case0; case[1] := Case1; case[2] := Case2; choice := 2; case[choice](); Texts.Append(Oberon.Log, W.buf) END Test;
BEGIN Texts.OpenWriter(W) END Cases.
Cases.Test
-- Message composed and transmitted by software designed to avoid the complication and vulnerability of antivirus software.
123456789 123456789 123456789 123456789 123456789 123456789 123456789 Pender Is., BC: +1 604 670 0140 Washington State: +1 360 639 0202 http://easthope.ca/Peter.html Bcc: peter at easthope. ca -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
participants (4)
-
Chris Burrows -
Jörg -
Jörg Straube -
peter@easthope.ca