Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #54539
| From | "HAA" <someone@microsoft.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | CASEs compared |
| Date | 2017-01-30 22:34 +1100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <o6n8ad$191s$1@gioia.aioe.org> (permalink) |
hughaguilar96@gmail.com wrote:
> ...
> CASE has to be nested IF statements though, which is horribly inefficient.
Perhaps not. Below is some test code for VFX. It compares IF/ELSE Case with
SWITCH from FD 20/3. The former gave me 10x better speed. In the FD article
the author states:
"The execution-time behavior of CASE and OF can be optimized until your system
implementor is exhausted and performance will be similar to that of the C version."
The reference to CASE aside (it has no run-time cost) I agree the performance of
IF/ELSE is probably as good as it gets for the general Case. ENDOF can begin
costing memory if there are many but in that event I would re-organize the code
to use END (assuming that's an option).
\ CASEs compared
( empty) forth definitions decimal
10000000 constant TIMES \ # loops
include timers.fth
: .timer ( ms -- ) Counter swap - u. ." mS " ;
\ case code from SWITCHBLADE FD20/3
\ Replace LINK@ and LINK, with your favorite list building words.
\ These are the methods used by SwiftForth.
\ : LINK@ @REL ;
\ : LINK, HERE OVER @REL ,REL SWAP !REL ;
synonym LINK@ @
: LINK, HERE OVER @ , SWAP ! ;
: SWITCHER ( i*x n head -- j*x )
DUP CELL+ @ >R ( save default xt)
BEGIN
LINK@ ?DUP WHILE ( n a)
2DUP CELL+ @ = IF ( match)
NIP CELL+ CELL+ @ EXECUTE
R> DROP EXIT
THEN
REPEAT R> EXECUTE ;
: <SWITCH ( head xt n -- head ) 2 PICK LINK, , , ;
: [SWITCH ( -- head ) CREATE HERE 0 , ' , DOES> SWITCHER ;
: SWITCH] ( head -- ) DROP ;
: RUN: ( head n -- )
:NONAME [CHAR] ; PARSE EVALUATE POSTPONE ; ( xt) SWAP <SWITCH ;
: (test1)
case
0 of endof 1 of endof 2 of endof 3 of endof
4 of endof 5 of endof 6 of endof 7 of endof
8 of endof 9 of endof
endcase ;
: test1 cr ." ANS CASE - "
Counter TIMES 0 do 9 (test1) loop .timer ;
\ labels in reverse order because last is compared first
[SWITCH (test2) NOOP
9 RUN: ; 8 RUN: ; 7 RUN: ; 6 RUN: ;
5 RUN: ; 4 RUN: ; 3 RUN: ; 2 RUN: ;
1 RUN: ; 0 RUN: ;
SWITCH]
: test2 cr ." SWITCH FD20/3 - "
Counter TIMES 0 do 9 (test2) loop .timer ;
\ end
Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread
CASEs compared "HAA" <someone@microsoft.com> - 2017-01-30 22:34 +1100
Re: CASEs compared anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-01-30 15:56 +0000
Re: CASEs compared hughaguilar96@gmail.com - 2017-01-30 09:57 -0800
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-01-31 14:26 +1100
Re: CASEs compared hughaguilar96@gmail.com - 2017-01-31 08:12 -0800
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-02-01 11:39 +1100
Re: CASEs compared hughaguilar96@gmail.com - 2017-01-31 22:37 -0800
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-01 12:26 -0800
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-01 16:50 -0800
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-01 18:02 -0800
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-02-02 21:12 +1100
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-02 13:46 -0800
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-02 17:01 -0800
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-02-04 12:43 +1100
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-03 17:52 -0800
Re: CASEs compared Alex <alex@rivadpm.com> - 2017-02-04 11:10 +0000
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-02 19:57 -0500
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-02 17:05 -0800
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-02 21:55 -0500
Re: CASEs compared Mark Wills <markwills1970@gmail.com> - 2017-02-03 02:11 -0800
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-03 10:52 -0800
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-03 17:38 -0800
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-03 22:08 -0500
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-03 21:40 -0800
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-04 01:16 -0500
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-03 14:40 -0500
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-03 17:47 -0800
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-03 22:04 -0500
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-02-04 12:39 +1100
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-03 23:32 -0500
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-02-04 22:50 +1100
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-04 11:07 -0500
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-04 08:48 -0800
Re: CASEs compared Mark Wills <markwills1970@gmail.com> - 2017-02-04 10:01 -0800
Re: CASEs compared rickman <gnuarm@gmail.com> - 2017-02-04 16:43 -0500
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-04 18:53 -0800
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-02-05 19:12 +1100
Re: CASEs compared "HAA" <someone@microsoft.com> - 2017-01-31 14:25 +1100
Re: CASEs compared Elliott Chapin <elliott.chapin@gmail.com> - 2017-02-05 08:25 -0800
Re: CASEs compared Coos Haak <htrofhc@gmail.com> - 2017-02-06 00:42 +0100
Re: CASEs compared jacko <jackokring@gmail.com> - 2017-02-16 17:46 -0800
Re: CASEs compared hughaguilar96@gmail.com - 2017-02-17 09:09 -0800
csiph-web