[Oberon] Numeric CASE vs IF THEN ELSE Performance
joerg.straube at iaeth.ch
Sat Jun 16 12:44:57 CEST 2018
> The Soundex-inspired example code included there has an Astrobe compiler
> listing file which shows the actual code generated for each statement in a
> disassembler-style listing, and a screenshot of the output showing the
> actual results running on an Arty development board.
> If you look at the disassembler listing you will see that another CASE
> benefit (particularly useful in embedded systems) is that it uses
> significantly less code space than IF THEN ELSE as long as the selections
> are not too sparse. The total code generated for each version of the Soundex
> procedures is:
> IF-THEN-ELSE: 420 Bytes
> CASE: 240 Bytes
For the special case of Soundex, neither IF-THEN nor CASE are perfect as we don’t want to execute statements per character but we want to map characters to their phonetic type: labial (1), guttural (2), dental-mute (3), palatal (4), nasal (5), dental-fricative (6) and vowels/silent (0).
So, an ARRAY is the best (speed and storage-wise) data structure to hold that mapping.
PROCEDURE SoundexArray(ch: CHAR): INTEGER;
RETURN ORD( map[ ORD(ch)-ORD("A") ] ) - ORD("0")
BEGIN (* ABCDEFGHIJKLMNOPQRSTUVWXYZ *)
map := "01230120022455012623010202"
ARRAY: 56 Bytes
one-time initialization + string constant: 63 Bytes
BTW: the runtime behaviour of the IF implementation can be optimized a little bit by rearranging the different IFs / ORs by taking the letter frequency into account. (vowels: 49%, dental-mute: 13%, guttural: 12%, nasal: 9%, labial: 7%, dental-fricative: 6%, palatal: 4%).
The code size can shrink a little by using an ELSE to combine the eight gutturals.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Oberon