Sujet : Re: May the numbers speak - supplement
De : mhx (at) *nospam* iae.nl (mhx)
Groupes : comp.lang.forthDate : 13. Jun 2025, 21:22:32
Autres entêtes
Organisation : novaBBS
Message-ID : <dfc8aa95bbcbee91152a846de77d932f@www.novabbs.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : Rocksolid Light
On Fri, 13 Jun 2025 17:47:24 +0000, Paul Rubin wrote:
mhx@iae.nl (mhx) writes:
HMS is three times faster than dtimescan and timestrscan, while
HMS2 is 3.5 times slower (as expected).
>
Is HMS the one that I posted, and HMS2 the version that's almost the
same? Why the speed difference: just because of the extra memory
traffic of leaving extra things on the stack?
HSM see below.
HSM2 is from dxf and uses >NUMBER . That is the kitchen sink compared
to '60 * ...' so of course it is much slower with the current
optimization level of iForth.
-marcel
-- ----------------------------------------------------------------------ANEW -timescan: dtimescan ( addr count -- d ) over swap + >r >r 0. r> 0
begin
over r@ <
while
over c@ ':'
= if swap >r s>d d+ #60 1 m*/ r> 0
else #10 * over c@ '0' - +
endif
swap 1+ swap
repeat
-r nip s>d d+ ;
: timestrscan ( addr count -- d )
1 1 LOCALS| c6 c1 |
0. 2SWAP
OVER + 1- ?DO
I C@ DUP ':' =
IF DROP
c6 #60 * TO c6
1 TO C1
ELSE '0' - C1 * C6 M* D+
#10 TO C1
ENDIF
-1 +LOOP ;
variable p
: advance 1 p +! ;
: digit ( -- n ) p @ c@ '0' - advance ;
: 2digit ( -- n ) digit #10 * digit + advance ; \ skips trailing colon
: hms ( a u -- n ) drop p ! 2digit #60 * 2digit + #60 * 2digit + ;
: (number) ( adr len -- ud adr' len' ) 0. 2swap >number ;
: get ( adr len -- u adr' len' ) (number) rot drop 1 /string ;
: hms2 ( adr len -- h m s) get get get 2drop ;
: TEST ( -- )
CR ." \ dtimescan : " S" 12:34:56" TICKS-RESET dtimescan
TICKS? UD. ." clock ticks elapsed, " UD.
CR ." \ timestrscan : " S" 12:34:56" TICKS-RESET timestrscan
TICKS? UD. ." clock ticks elapsed, " UD.
CR ." \ HMS : " S" 12:34:56" TICKS-RESET hms
TICKS? UD. ." clock ticks elapsed, " .
CR ." \ HMS2 : " S" 12:34:56" TICKS-RESET hms2
TICKS? UD. ." clock ticks elapsed, " ROT . SWAP . . ;
TEST
#2000000 VALUE #iters
CREATE num ," 12:34:56"
: .SECS MS? #1000000 #iters 3 * */ ." / " . ." ns per iteration." ;
: TEST2 ( -- )
CR
CR ." \ dtimescan : " TIMER-RESET num C@+ #iters 0 ?DO 2DUP
dtimescan 2DROP 2DUP dtimescan 2DROP 2DUP dtimescan 2DROP
LOOP
dtimescan UD. .SECS
CR ." \ timestrscan : " TIMER-RESET num C@+ #iters 0 ?DO 2DUP
timestrscan 2DROP 2DUP timestrscan 2DROP 2DUP timestrscan 2DROP
LOOP
timestrscan UD. .SECS
CR ." \ HMS : " TIMER-RESET num C@+ #iters 0 ?DO 2DUP
hms DROP 2DUP hms DROP 2DUP hms DROP
LOOP
hms . .SECS
CR ." \ HMS2 : " TIMER-RESET num C@+ #iters 0 ?DO 2DUP
hms2 3DROP 2DUP hms2 3DROP 2DUP hms2 3DROP
LOOP
hms2 ROT . SWAP . . .SECS ;
TEST2