Deutsch   English   Français   Italiano  
<nnd$4e76ec17$5bf3f2dc@a5a0e448bbfdac5e>

View for Bookmarking (what is this?)
Look up another Usenet article

Date: Mon, 30 Jun 2025 15:43:22 +0200
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: OOS approach revisited
Newsgroups: comp.lang.forth
References: <fdbeb48de8ceb748d44c67dc3981a566@www.novabbs.com>
 <84d259e0f1d6210d84c7840af5d51f4ebdd71ed4@i2pn2.org>
 <c9158c200102c2e508f8ef11deecdc27@www.novabbs.com>
 <248abae6393c59470a015b195642e266@www.novabbs.com>
Content-Language: en-US
From: Hans Bezemer <the.beez.speaks@gmail.com>
In-Reply-To: <248abae6393c59470a015b195642e266@www.novabbs.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Message-ID: <nnd$4e76ec17$5bf3f2dc@a5a0e448bbfdac5e>
Organization: KPN B.V.
Path: news.eternal-september.org!eternal-september.org!feeder3.eternal-september.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feed.abavia.com!abe004.abavia.com!news.kpn.nl!not-for-mail
Lines: 179
Injection-Date: Mon, 30 Jun 2025 15:43:22 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"

On 27-06-2025 04:16, minforth wrote:
> On Thu, 26 Jun 2025 17:27:48 +0000, LIT wrote:
> 
>>> The saving come from rolling  @ @ + ! into a single very specialized
>>> function.  But what about the loading of X Y and retrieving of Z which
>>> are unavoidable in practice?  Should that not be included in the test?
>>
>> Let's find out then:
>>
>> 1 VARIABLE X
>> 2 VARIABLE Y
>> 3 VARIABLE Z
>>
>> : TEST1 1000 0 DO 10000 0 DO
>>     I DUP X ! Y ! X @ Y @ + Z ! Z @ DROP
>>   LOOP LOOP ;
>> : TEST2 1000 0 DO 10000 0 DO
>>     I DUP X ! Y ! X Y Z +> Z @ DROP
>>   LOOP LOOP ;
>> TICKS TEST1 TICKS 2SWAP DMINUS D+ D. 252 ok
>> TICKS TEST2 TICKS 2SWAP DMINUS D+ D. 202 ok
>>
>> : TEST1 1000 0 DO 10000 0 DO
>>     I DUP X ! Y ! 1 X +! 1 Y +! X @ Y @ + Z ! Z @ DROP
>>   LOOP LOOP ;
>> : TEST2 1000 0 DO 10000 0 DO
>>     I DUP X ! Y ! X ++ Y ++  X Y Z +> Z @ DROP
>>   LOOP LOOP ;
>> TICKS TEST1 TICKS 2SWAP DMINUS D+ D. 346 ok
>> TICKS TEST2 TICKS 2SWAP DMINUS D+ D. 258 ok
>>
>> The difference is smaller - still it's significant.
>>
>>
>> Another test - using the "drawing a box" example
>> from "Thinking Forth" (and "simulated" LINE word):
>>
>> 0 VARIABLE TOP
>> 0 VARIABLE LEFT
>> 0 VARIABLE BOTTOM
>> 0 VARIABLE RIGHT
>> : LINE 2DROP 2DROP ;
>>
>> : BOX1 ( x1 y1 x2 y2) BOTTOM ! RIGHT ! TOP ! LEFT !
>>  LEFT  @ TOP    @ RIGHT @ TOP    @ LINE
>>  RIGHT @ TOP    @ RIGHT @ BOTTOM @ LINE
>>  RIGHT @ BOTTOM @ LEFT  @ BOTTOM @ LINE
>>  LEFT  @ BOTTOM @ LEFT  @ TOP    @ LINE ;
>>
>> : BOX2 ( x1 y1 x2 y2) BOTTOM ! RIGHT ! TOP ! LEFT !
>>  LEFT  TOP    RIGHT TOP    LINE
>>  RIGHT TOP    RIGHT BOTTOM LINE
>>  RIGHT BOTTOM LEFT  BOTTOM LINE
>>  LEFT  BOTTOM LEFT  TOP    LINE ;
>>
>> : TEST1 1000 0 DO 10000 0 DO  I DUP 2DUP BOX1  LOOP LOOP ;
>> : TEST2 1000 0 DO 10000 0 DO  I DUP 2DUP BOX2  LOOP LOOP ;
>>
>> TICKS TEST1 TICKS 2SWAP DMINUS D+ D. 890 ok
>> TICKS TEST2 TICKS 2SWAP DMINUS D+ D. 653 ok
>>
>>
>> The difference is even more significant in case
>> of multiplication:
>>
>> 1 VARIABLE X
>> 2 VARIABLE Y
>> 3 VARIABLE Z
>>
>> : TEST1 1000 0 DO 10000 0 DO
>>     I DUP X ! Y ! X @ Y @ * Z ! Z @ DROP
>>   LOOP LOOP ;
>> : TEST2 1000 0 DO 10000 0 DO
>>     I DUP X ! Y ! X Y Z *> Z @ DROP
>>   LOOP LOOP ;
>> TICKS TEST1 TICKS 2SWAP DMINUS D+ D. 658 ok
>> TICKS TEST2 TICKS 2SWAP DMINUS D+ D. 200 ok
>>
>> But this time better implementation has also
>> its impact; fig-Forth's '*' is inefficient,
>> and I coded '*>' of course directly in ML,
>> simply using IMUL.
>>
> 
> With so many DO..LOOPs involved, be careful not to
> measure more looping time than the multiplications.
> 
> IIRC DO..LOOPs had been a hack for computers in the 60s.
> A rather ugly hack, born out of necessity, slow and
> often cumbersome to use. That it still persists in Forth
> half a century later speaks for Forth's progressiveness.
> 
> -- 

Not the most beautiful code, but enough stuff to test:

: LINE 2DROP 2DROP ;
\ include lib/anstools.4th

0 [if]
VARIABLE TOP
VARIABLE LEFT
VARIABLE BOTTOM
VARIABLE RIGHT

: BOX ( x1 y1 x2 y2) BOTTOM ! RIGHT ! TOP ! LEFT !
   LEFT  @ TOP    @ RIGHT @ TOP    @ LINE
   RIGHT @ TOP    @ RIGHT @ BOTTOM @ LINE
   RIGHT @ BOTTOM @ LEFT  @ BOTTOM @ LINE
   LEFT  @ BOTTOM @ LEFT  @ TOP    @ LINE ;
[then]

1 [if]
aka r@   bottom
aka r'@  top

\ left right bottom top
: BOX ( x1 y1 x2 y2)
   rot >r >r
   over over top tuck line
   dup top over bottom line
   over bottom tuck line
   r> over r> line
;
[then]

hide bottom
hide top

\ 1 2 4 8 box
: TEST1 1000 0 DO 10000 0 DO  I DUP 2DUP BOX  LOOP LOOP ;
  test1

\ 1 2 4 2 (TOS)
\ 4 2 4 8 (TOS)
\ 4 8 1 8 (TOS)
\ 1 8 1 2 (TOS)

Variable version: 0.950s
Stack version: 0.848s

Note: 4tH has *THREE* directly addressable Return Stack items. I used 
this to implement the Midpoint Circle algorithm in 4tH. So I could have 
made it even a bit more efficient.

Note 4tH optimizes variables by appending the VALUE C-behavior to 
(known) variables:

      6| branch                             30   BOX
      7| to                                  2
      8| to                                  3   RIGHT
      9| to                                  0
     10| to                                  1   LEFT
     11| value                               1   LEFT
     12| value                               0
     13| value                               3   RIGHT
     14| value                               0
     15| call                                0   LINE
     16| value                               3   RIGHT
     17| value                               0
     18| value                               3   RIGHT
     19| value                               2
     20| call                                0   LINE
     21| value                               3   RIGHT
     22| value                               2
     23| value                               1   LEFT
     24| value                               2
     25| call                                0   LINE
     26| value                               1   LEFT
     27| value                               2
     28| value                               1   LEFT
     29| value                               0
     30| branch                              0   LINE

Using variables is *not* deliberately slow.

Hans Bezemer