I have attached two copies of a CASE statement which implements part of the Soundex algorithm. Assume that both compile OK but both have at least one identical logical error which you may not have noticed if I hadn't warned you. Which version do you think would be more effective in flushing out the error if the programmer was not aware of his mistake? I put my money on the one without the ELSE . Regards, Chris Burrows CFB Software http://www.astrobe.com/RISC5
Okay. Q and T are not mentioned and will be trapped. If you know that for Q and T the value should be 0 then why not mention them explicitly. CASE is meant as an implementation optimization with a jump table instead of several IFs. With a jump table you have the in-table cases and the out-of-table case. Should ELSE cover both or only in-table or only out-of-table? We really have to see, how many of us did detect that the numeric CASE is not implemented (yet) in ORP.Mod? I assume there is only a handful (either me as being interested in compiler construction or the very few actually trying to use numeric CASE) A word to speed benefit: Even if you have - lets say - 128 case labels you would need only 7 IFs at runtime (with binary division). So, the benefit of having a jump table is rather small. Dont get me wrong: numeric CASE is nice and syntactically pleasing, but how often do you need it? br Jörg From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of Chris Burrows Sent: Friday, February 16, 2018 12:36 PM To: ETH Oberon and related systems <oberon@lists.inf.ethz.ch> Subject: [Oberon] A CASE quiz I have attached two copies of a CASE statement which implements part of the Soundex algorithm. Assume that both compile OK but both have at least one identical logical error which you may not have noticed if I hadnt warned you. Which version do you think would be more effective in flushing out the error if the programmer was not aware of his mistake? I put my money on the one without the ELSE Regards, Chris Burrows CFB Software http://www.astrobe.com/RISC5
-----Original Message----- From: Jörg [mailto:joerg.straube@iaeth.ch] Sent: Friday, 16 February 2018 10:37 PM To: chris@cfbsoftware.com; 'ETH Oberon and related systems' Subject: RE: [Oberon] A CASE quiz
Okay. Q and T are not mentioned and will be trapped.
Good effort but only 5/10 I'm afraid ;-) You were right about "Q" but "T" IS there. You have inadvertently helped to illustrate that even skilled programmers make these sorts of mistakes.
If you know that for Q and T the value should be 0 then why not mention them explicitly.
That's the whole point of the quiz. "Q" should be 2 but the programmer forgot it altogether. The ELSE swallows it up without it being noticed (!!!), but the NoElse results in a runtime error so alerts the programmer that something needs fixing. The earlier in the software lifecycle that an error is detected, the cheaper it is to fix it.
We really have to see, how many of us did detect that the numeric CASE is not implemented (yet) in ORP.Mod?
It has been previously discussed here a couple of years ago: http://lists.inf.ethz.ch/pipermail/oberon/2015/008508.html Regards, Chris Chris Burrows CFB Software http://www.astrobe.com/RISC5
The bug arises because the programmer has tried to hand-optimise the code at the expense of clarity. Prevention is better than cure. My approach to this, regardless of whether or not CASE has an ELSE clause, would have been CASE ch of "A" : value := 0 | "B" : value := 1 | "C" : value := 2 | "D" : value := 3 | "E" : value := 0 | and so on. In this way it can be seen quite clearly if any input values are missing, and the duplication of output values is also apparent. Let the compiler do the optimising. B
-----Original Message----- From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of Chris Burrows Sent: Friday, 16 February, 2018 12:30 PM To: ETH Oberon and related systems <oberon@lists.inf.ethz.ch> Subject: Re: [Oberon] A CASE quiz
-----Original Message----- From: Jörg [mailto:joerg.straube@iaeth.ch] Sent: Friday, 16 February 2018 10:37 PM To: chris@cfbsoftware.com; 'ETH Oberon and related systems' Subject: RE: [Oberon] A CASE quiz
Okay. "Q" and "T" are not mentioned and will be trapped.
Good effort but only 5/10 I'm afraid ;-) You were right about "Q" but "T" IS there. You have inadvertently helped to illustrate that even skilled programmers make these sorts of mistakes.
If you know that for "Q" and "T" the value should be 0 then why not mention them explicitly.
That's the whole point of the quiz. "Q" should be 2 but the programmer forgot it altogether. The ELSE swallows it up without it being noticed (!!!), but the NoElse results in a runtime error so alerts the programmer that something needs fixing.
The earlier in the software lifecycle that an error is detected, the cheaper it is to fix it.
We really have to see, how many of us did detect that the numeric CASE is not implemented (yet) in ORP.Mod?
It has been previously discussed here a couple of years ago:
http://lists.inf.ethz.ch/pipermail/oberon/2015/008508.html
Regards, Chris
Chris Burrows CFB Software http://www.astrobe.com/RISC5
-- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
On Fri, Feb 16, 2018, at 1:07 PM, Jörg wrote:
A word to speed benefit: Even if you have - let’s say - 128 case labels you would need only 7 IFs at runtime (with binary division). So, the benefit of having a jump table is rather small. Don’t get me wrong: numeric CASE is nice and syntactically pleasing, but how often do you need it?
About performance consideration: http://bernd-oppolzer.de/CaseStmts.pdf The figure on page 10 is very interesting
-----Original Message----- From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of Diego Sardina Sent: Friday, 16 February 2018 11:04 PM To: oberon@lists.inf.ethz.ch Subject: Re: [Oberon] A CASE quiz
On Fri, Feb 16, 2018, at 1:07 PM, J rg wrote:
About performance consideration: http://bernd- oppolzer.de/CaseStmts.pdf
The figure on page 10 is very interesting
Thank you for the interesting link. I have some less scientific observations to make about my RISC5 implementation of the case statement: The increase in size of the compiler sources is about 250 lines of code, i.e. an increase of 7%. It is a non-trivial addition. The increase in size of the compiler executable is 2.2KB, an increase of 4%. Compiles are typically about 10% faster after implementing the PO2013 Oberon compiler scanner function ORS.Get as a CASE statement. The number of lines of code in ORS reduced by about 3% as a result. Regards, Chris Burrows CFB Software http://www.astrobe.com/RISC5
On Fri, Feb 16, 2018, at 2:04 PM, Chris Burrows wrote:
Compiles are typically about 10% faster after implementing the PO2013 Oberon compiler scanner function ORS.Get as a CASE statement. The number of lines of code in ORS reduced by about 3% as a result.
It's worthy to say that Wirth's scanner with IFs is optimised to reduce the number of tests per character. They are also ordered on the frequency in an Oberon source code. Of course, a flatter IF sequence would be a lot slower. However, I'm very surprised about that 10%. I expect a lot more since the scanner is the most sensible part in a single-pass compiler. In the past I wrote a very efficient scanner that used the CASE statement as the main selector and various lookup tables to reduce the time of scanning, something like this: (* all elements must be initialised to true *) VAR nonAlphaNumerics : ARRAY 256 OF BOOLEAN; VAR nonWhiteSpaces : ARRAY 256 OF BOOLEAN; nonAlphaNumerics[ORD("A")] := FALSE; nonAlphaNumerics[ORD("B")] := FALSE; [...] nonWhiteSpaces[ORD(20X)] := FALSE; (* TAB *) nonWhiteSpaces[ORD(09X)] := FALSE; (* Blank space *) [...] In the identifier scanner all the tests were simplified in: REPEAT [...] UNTIL nonAlphaNumerics[ORD(ch)]; instead of: REPEAT [...] UNTIL (ch < "0") OR (ch > "9") & (ch < "A") OR (ch > "Z") & (ch < "a") OR (ch > "z"); With CASE statement as main selector and lookup tables for scanning I achieved a truly constant time (the number of characters in the text), while the equivalent with a lot of tests was slower in a range of 70% to 300%, based on the text scanned. These were outstanding results. I reduced memory requirement by using SETs instead of ARRAYs, with a small impact on speed (I don't remember how much in detail in this moment). Thinking more about it, the speed benefit of jump and lookup tables are very sensible to the computer architecture, a very small and slow cache/memory could be an issue for these techniques. So depending on the architecture the benefits of jump and lookup tables are not cost effective. It would be nice to test these techniques in a more limited architecture. In my opinion, however, the CASE statement is fundamental, also if its performance depends on various factor.
On Sat, Feb 17, 2018, at 3:29 PM, Diego Sardina wrote:
VAR nonAlphaNumerics : ARRAY 256 OF BOOLEAN; VAR nonWhiteSpaces : ARRAY 256 OF BOOLEAN;
nonAlphaNumerics[ORD("A")] := FALSE; nonAlphaNumerics[ORD("B")] := FALSE; [...]
An interesting note, Modula-3 permits this declaration: VAR nonAlphaNumerics : ARRAY CHAR OF BOOLEAN; With this form, it's possible to use nonAlphaNumerics["B"] without a type conversion on the index
participants (4)
-
Bob Walkden -
Chris Burrows -
Diego Sardina -
Jörg