From: Ralph Ronnquist Date: Fri, 11 Jun 2021 10:14:31 +0000 (+1000) Subject: another example X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=57e9b9cc817399bf2d27601e6a4d841ba2318c8c;p=rrq%2Frrqforth.git another example --- diff --git a/examples/timing.f b/examples/timing.f new file mode 100755 index 0000000..61d5c0c --- /dev/null +++ b/examples/timing.f @@ -0,0 +1,36 @@ +#!/home/ralph/src/devuan/rrqforth/rrqforth +# +# This example adds the words: +# time0 = variable for holding a time stamp +# time = function to read of current time; seconds since epoch +# marktime = function that sets time0 from current time +# telltime = function that prints the difference between current time and time0 +# longloop = test function for simple timing test + +INPUT @ READ-WORD time0 CREATE DROP 0 , ( variable time0 ) + +: time ( -- s ; seconds since epoch ) + 0 [ SYSTEM USE SYS_TIME TFA>CFA , ] +; + +: marktime ( -- ; set time0 from current time ) + time time0 ! +; + +: telltime ( -- s ; read off time since time0 ) + time time0 @ - . +; + +: longloop ( n -- ) + S" Takes " TELL marktime + 0 BEGIN + 1 + 2DUP <= IFBREAK + AGAIN 2DROP + telltime S" seconds" TELL NL EMIT +; + +1000000000 4 / +" Test run a tight loop of " TELL DUP . " cycles" TELL NL EMIT +longloop + +0 EXIT