Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The sum of the Universe is zero.


devel / comp.lang.forth / system clock elapsed time accuracy

SubjectAuthor
* system clock elapsed time accuracyKrishna Myneni
`* Re: system clock elapsed time accuracyKrishna Myneni
 `- Re: system clock elapsed time accuracyKrishna Myneni

1
system clock elapsed time accuracy

<subp5m$q8k$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16772&group=comp.lang.forth#16772

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: system clock elapsed time accuracy
Date: Sun, 13 Feb 2022 14:20:04 -0600
Organization: A noiseless patient Spider
Lines: 271
Message-ID: <subp5m$q8k$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 13 Feb 2022 20:20:06 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a1168a35ab384b788766a7cbeb152235";
logging-data="26900"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MndZ7EdFubvEbDamJhpLE"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:0J+3xEUd8esiL5FlOP2j4gbe6TQ=
Content-Language: en-US
 by: Krishna Myneni - Sun, 13 Feb 2022 20:20 UTC

In an earlier thread, the timing requirements for benchmarking code
using a non-standard word such as MS@ (from kForth) was brought up. It
was claimed that accuracy at +/-1 ms could be obtained using a
benchmarking sequence such as

MS@ ( ... code to be benchmarked ) MS@ SWAP -

which leaves the elapsed time in milliseconds on the stack for the
benchmarked code execution.

The accuracy of the elapsed time depends on the accuracy of the system
clock, which will in general vary with the duration (elapsed time). I
claimed that a computer obtaining its time from a time server should be
capable of achieving millisecond accuracy. I decided to see how well the
system clock for a stand-alone machine (not connected to a network and
having no special time reference input) would do in terms of accuracy of
measuring the elapsed time using the sequence above.

In order to do this, I need an accurate external timer and a way to
start and stop the timer. Several years ago, I bought a used HP 5341 A
universal timer/counter from ebay for about $100 USD (a very low price).
Although this timer/counter does not have a computer interface, it has a
very accurate timebase, using a TXCO (temperature-compensated crystal
oscillator) and can readily realize accuracy of 1 microsecond or better
when calibrated.

I decided to use an old PC with a RS232 serial port interface and use
one of the control lines (DTR or RTS), which can be raised or lowered
using the serial port interface module in kForth, to start and stop the
time for a single shot measurement. The only difficult part of getting
this to work was having to rig up a cable, with a voltage divider to
bring the RS232 voltage of the control line into a low enough range for
the input to the timer/counter. Details of this are in the software
listing below.

The following sequence allowed me to make an accurate external
measurement of the time elapsed:

RAISE-LINE MS@ ( code ... ) LOWER-LINE MS@ SWAP -

There are some additional details and restrictions dealing with the
quirks of the instrumentation, but essentially the above sequence allows
me to measure both the system clock elapsed time and make a nearly
simultaneous external measurement using a high accuracy external timer.
The raw data is given below. In my case the code to be benchmarked was
simply a fixed delay using the standard word MS . Therefore, the
measurement requires specifying a desired delay, measuring the elapsed
time on the system clock (using MS@), and measuring the elapsed time on
the external timer. The important comparison is between the elapsed time
on the system clock and the elapsed time measured by the external timer.

For delays from 1 to 50 milliseconds (code which executes within this
range), the system clock elapsed time and the external timer elapsed
time were within 1 ms of each other, and usually within 35 microseconds
of each other, except in one of the measurements. Such spurious
discrepancies may occur more frequently for intervals longer than 50 ms.
With statistical averaging, it appears that benchmarking accuracy at sub
milli-second accuracy can be achieved using the system clock over 1--50
ms intervals, even for some stand-alone systems not having network time
synchronization or other external reference sources.

--
Krishna

Source code for DeltaT.4th
( Requires the latest version of serial.4th from the kForth-32
or kForth-64 repositories on GitHub:
https://github.com/mynenik/kForth-64/blob/master/forth-src/serial.4th
) ---
\ DeltaT.4th
\ \ Compare time interval measurement using system clock
\ with external high accuracy time interval measurement
\ instrument such as the HP5314A universal counter timer.
\ \ This code allows generation of a rectangular pulse through
\ the computer's serial port DTR line (pin 4) or RTS line
\ (pin 7), and Ground (pin 5). The duration of the pulse may
\ be measured using an external high accuracy/high precision
\ timer, while the start and stop time of the pulse are also
\ measured by reading the system clock.
\ \ Note RS-232 line voltage can vary between -24V to +24V. A
\ voltage divider and level shifter may be required to couple
\ the signal into an external measurement device.
\ \ I use the HP5314A counter/timer which has ac coupled inputs.
\ Therefore, the only signal conditioning needed is a simple
\ voltage divider of about 5, which can be formed with two
\ low resistors. I used 2.2 Kohm and 450 ohm 1/4 W resistors
\ to attenuate the RTS line signal into the counter/timer
\ input.
\ \ HP5314A set up for single-shot time interval measurement using
\ voltage-divided DTR/RTS line (see manual Fig. 3-5):
\ \ Connect pulse line to Input A
\ Shift button (blue) In
\ Per A/TI A->B button In
\ Norm/Hold button Out (Norm)
\ Resolution/N=1 button In (single shot mode)
\ Input A Slope button Out (positive slope)
\ Input A Attenuation button In (x20)
\ Input B Slope button In (negative slope)
\ SEP/COM A button In (COM A)
\ \ Requires:
\ \ ans-words.4th
\ modules.4th
\ struct.4th
\ struct-ext.4th
\ strings.4th
\ serial.4th
\

include ans-words
include modules
include struct
include struct-ext
include strings
include serial

Also serial

base @
decimal

defer raise-line
defer lower-line
' raise-rts IS raise-line
' lower-rts IS lower-line

\ alternately one can use RAISE-DTR and LOWER-DTR

variable com

: open-it ( -- ior )
COM1 ∋ serial open com !
com @ 0> if
com @ c" 8N1" set-params
com @ B57600 set-baud
com @ ∋ serial flush
0
else 1 then
com @ lower-line
;

: close-it ( -- ior ) com @ ∋ serial close ;

\ Generate continuous waveform at 2 ms period, for
\ viewing on an oscilloscope.
: cw ( -- )
open-it Abort" Unable to open com port!"
BEGIN
com @ raise-line
1 ms
com @ lower-line
1 ms
key?
UNTIL
key drop
close-it
;

\ Measure system time elapsed and provide a pulse
\ to an external measurement device, e.g. HP 5314 A.
\ This counter/timer appears to require a short
\ heralding pulse (1 ms long here) prior to the
\ pulse being measured.
\ \ ms1 is the specified pulse duration
\ ms2 is the measured interval from the system clock
\ The external counter/timer measurement must be read
\ and recorded manually, unless it has a computer
\ interface -- the HP 5314 A does not.
: gen-pulse ( ms1 -- ms1 ms2 )
com @ raise-line
1 ms com @ lower-line
1 ms com @ raise-line
ms@
over ms
com @ lower-line
ms@
swap -
;

base !
---

Raw data obtained using GEN-PULSE
Specified Interval (ms), Sys Clock Elapsed (ms), Ext. Timer (microseconds)
---
1 1 1010.8
1 1 1011.2
1 1 1011.8
1 1 1011.0
1 1 1015.5
1 1 1011.6
1 1 1011.3
1 1 1011.3
1 1 1010.9
---
2 2 2011.9
2 3 2011.5
2 2 2012.0
2 2 2011.3
2 2 2011.5
2 2 2015.6
2 2 2011.2
2 2 2012.3
2 2 2011.4
2 2 2011.0
---
5 5 5012.5
5 5 5025.0
5 5 5013.0
5 5 5012.6
5 5 5031.3
5 5 5032.3
5 5 5030.9
5 5 5013.2
5 5 5012.7
5 5 5034.7
---
10 10 10,031.5
10 10 10,013.5
10 10 10,031.9
10 10 10,034.7
10 10 10,033.0
10 10 10,015.4
10 10 10,015.6
10 10 10,030.8
10 10 10,013.9
10 10 10,013.3
---
20 20 20,033.6
20 20 20,019.3
20 20 20,035.9
20 20 20,036.3
20 20 20,029.7
20 20 20,031.8
20 20 20,023.1
20 20 20,025.4
20 20 20,033.5
---
30 30 30,032.5
30 30 30,031.0
30 31 30,033.5
30 30 30,031.0
30 30 30,055.9
30 30 30,032.3
30 30 30,035.8
30 30 30,025.5
30 30 30,031.5
30 30 30,032.0
---
50 50 50,031.7
50 54 51,000.0
50 50 50,022.7
50 50 50,049.3
50 50 50,036.3
50 50 50,030.6
50 50 50,065.6
50 50 50,037.0
50 50 50,025.2
50 50 50,059.7
---
fin

Re: system clock elapsed time accuracy

<suf9g9$gig$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16795&group=comp.lang.forth#16795

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: system clock elapsed time accuracy
Date: Mon, 14 Feb 2022 22:17:11 -0600
Organization: A noiseless patient Spider
Lines: 169
Message-ID: <suf9g9$gig$1@dont-email.me>
References: <subp5m$q8k$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 15 Feb 2022 04:17:13 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="046cd61d389e2105a60054c1b687a9ae";
logging-data="16976"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YPbyiWwezwT4lMpoUuGHK"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:mPr3+Z4aWnjGzywt0jjIvmsKExc=
In-Reply-To: <subp5m$q8k$1@dont-email.me>
Content-Language: en-US
 by: Krishna Myneni - Tue, 15 Feb 2022 04:17 UTC

On 2/13/22 14:20, Krishna Myneni wrote:
> In an earlier thread, the timing requirements for benchmarking code
> using a non-standard word such as MS@ (from kForth) was brought up. It
> was claimed that accuracy at +/-1 ms could be obtained using a
> benchmarking sequence such as
>
> MS@ ( ... code to be benchmarked ) MS@ SWAP -
>
> which leaves the elapsed time in milliseconds on the stack for the
> benchmarked code execution.
>

> ... I decided to see how well the
> system clock for a stand-alone machine (not connected to a network and
> having no special time reference input) would do in terms of accuracy of
> measuring the elapsed time using the sequence above.
> ...

Several improvements were made to yesterday's procedure. I can now
obtain the external measurement of time elapsed over a much longer
duration with the HP 5341 A. Also, I understand the necessity for the
initial heralding pulse for this particular instrument (read the manual,
section 3.9, Time Interval Measurement, and especially on arming the
instrument to respond to the start and stop edges in 3-11). Although the
instrument has a range of 1 sec, with 100 ns resolution for time
interval measurement, it is designed to nicely wrap the counter after
exceeding 1,000,000.0s, so the displayed digits for the time interval
are meaningful.

My limited range of 50 ms for time interval measurement yesterday was
due to not setting the trigger levels for the start and stop edges
properly for the source signal (the slopes were set properly).

With these hardware setup issues resolved, I also modified GEN-PULSE to
accept a time in microseconds. The system clock elapsed time is measured
in microseconds now, using kForth's US2@ which returns a double number
containing the microseconds elapsed from a reference time. We used MS to
simulate the benchmark code; however MS is a precision delay in kForth,
since it polls the system clock rather than putting the process to
sleep. For the present tests, over an elapsed time from about 65
thousand microseconds (65 milliseconds) to 34 million microseconds (34
seconds), I use kForth's system call USLEEP to put the process to sleep,
so there will be scatter in the elapsed times.

The revised GEN-PULSE is listed below (all other code is the same). For
those interested in the comparison of system clock elapsed time versus
elapsed time measured by an external timer/counter, I leave the raw data
below for you to peruse and draw your conclusions.

--
Krishna

Revised GEN-PULSE (for kForth64/32)
---
\ Measure system time elapsed and provide a pulse
\ to an external measurement device, e.g. HP 5314 A.

: gen-pulse ( us1 -- us2 )
com @ raise-line
1 ms com @ lower-line
1 ms com @ raise-line
us2@ \ ms@
rot usleep \ over ms
com @ lower-line
us2@ \ ms@
2swap d- \ swap -
; ---

Data
All times are in microseconds.
us1, System Clock Elapsed, HP Timer Meas.
---
65,536 65,682 65,680.5
........ .. 650 .. 647.4
........ .. 649 .. 647.5
........ .. 648 .. 645.3
........ .. 655 .. 653.0
........ .. 644 .. 642.0
........ .. 645 .. 643.1
........ .. 684 .. 679.7
........ .. 653 .. 651.1
........ .. 653 .. 650.4
---
131,072 131,183 131,181.7
........ ... 193 ... 190.4
........ ... 184 ... 182.5
........ ... 196 ... 193.4
........ ... 188 ... 185.8
........ ... 179 ... 177.3
........ ... 182 ... 179.3
........ ... 185 ... 183.4
........ ... 182 ... 180.2
........ ... 180 ... 177.8
---
262,144 262,287 262,284.8
........ ... 292 ... 289.7
........ ... 258 ... 255.8
........ ... 254 ... 251.8
........ ... 316 ... 263.2
........ ... 253 ... 251.8
........ ... 254 ... 252.6
........ ... 258 ... 255.5
........ ... 254 ... 252.4
........ ... 254 ... 251.6
---
524,288 524,395 524,393.5
........ ... 394 ... 392.3
........ ... 402 ... 400.8
........ ... 400 ... 398.0
........ ... 399 ... 397.1
........ ... 360 ... 358.4
........ ... 397 ... 395.9
........ ... 380 ... 378.5
........ ... 405 ... 403.1
........ ... 397 ... 395.0
---
1,048,576 1,048,685 [1,]048,682.6 * (Counter Wrap)
.......... ..... 703 ....... 700.4
.......... ..... 660 ....... 658.4
.......... ..... 681 ....... 678.6
.......... ..... 725 ....... 723.7
.......... ..... 674 ....... 672.5
.......... ..... 721 ....... 677.2 (44 us difference)
.......... ..... 711 ....... 709.3
.......... ..... 665 ....... 662.6
.......... ..... 686 ....... 683.8
---
8,388,608 8,388,723 [8,]388,721.2 * (Counter Wrap)
.......... ..... 717 ....... 713.6
.......... ..... 718 ....... 716.2
.......... ..... 714 ....... 712.1
.......... ..... 715 ....... 713.2
.......... ..... 717 ....... 715.3
.......... ..... 716 ....... 713.4
.......... ..... 718 ....... 716.6
.......... ..... 723 ....... 721.0
.......... ..... 705 ....... 702.8
---
16,777,216 16,777,306 [16,]777,303.3 * (Counter Wrap)
........... ...... 316 ........ 313.9
........... ...... 318 ........ 315.0
........... ...... 327 ........ 324.2
........... ...... 323 ........ 320.3
........... ...... 349 ........ 346.5
........... ...... 327 ........ 323.7
........... ...... 326 ........ 322.8
........... ...... 325 ........ 321.8
........... ...... 552 ........ 549.9
---
33,554,432 33,554,527 [33,]554,523.1 * (Counter Wrap)
........... ...... 538 ........ 534.2
........... ...... 538 ........ 534.4
........... ...... 535 ........ 530.7
........... ...... 547 ........ 543.9
........... ...... 517 ........ 514.5
........... ...... 523 ........ 519.5
........... ...... 529 ........ 525.9
........... ...... 540 ........ 536.6
........... ...... 539 ........ 536.3
---
fin

Re: system clock elapsed time accuracy

<suf9ip$gig$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16796&group=comp.lang.forth#16796

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: system clock elapsed time accuracy
Date: Mon, 14 Feb 2022 22:18:33 -0600
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <suf9ip$gig$2@dont-email.me>
References: <subp5m$q8k$1@dont-email.me> <suf9g9$gig$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 15 Feb 2022 04:18:33 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="046cd61d389e2105a60054c1b687a9ae";
logging-data="16976"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vYfHl9VLwCOIkmoAkxbwY"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:U6lGWj3hiyOQV07OCZRdTkMwPU4=
In-Reply-To: <suf9g9$gig$1@dont-email.me>
Content-Language: en-US
 by: Krishna Myneni - Tue, 15 Feb 2022 04:18 UTC

On 2/14/22 22:17, Krishna Myneni wrote:
> it is designed to nicely wrap the counter after exceeding 1,000,000.0s,

after 1,000,000.0 microseconds.

KM

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor