another example
[rrq/rrqforth.git] / examples / timing.f
1 #!/home/ralph/src/devuan/rrqforth/rrqforth
2 #
3 # This example adds the words:
4 # time0 = variable for holding a time stamp
5 # time = function to read of current time; seconds since epoch
6 # marktime = function that sets time0 from current time
7 # telltime = function that prints the difference between current time and time0
8 # longloop = test function for simple timing test
9
10 INPUT @ READ-WORD time0 CREATE DROP 0 , ( variable time0 )
11
12 : time ( -- s ; seconds since epoch )
13   0 [ SYSTEM USE SYS_TIME TFA>CFA , ]
14 ;
15
16 : marktime ( -- ; set time0 from current time )
17   time time0 !
18 ;
19
20 : telltime ( -- s ; read off time since time0 )
21   time time0 @ - .
22 ;
23
24 : longloop ( n -- )
25   S" Takes " TELL marktime
26   0 BEGIN
27     1 + 2DUP <= IFBREAK
28   AGAIN 2DROP
29   telltime S"  seconds" TELL NL EMIT
30 ;
31
32 1000000000 4 /
33 " Test run a tight loop of " TELL DUP . "  cycles" TELL NL EMIT
34 longloop
35
36 0 EXIT