Computing time elapsed, how?

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Marty
Posts: 4
Joined: Fri 04 Oct 2024, 05:13

Computing time elapsed, how?

Post by Marty »

Help please. Need to compute time elapsed between two events in execution of code.

Here a series of looping mathematics calculations are executed. I need display how long it takes to execute (typically seconds) at the end of execution.

Below the PowerBasic (also QBasic) code looks like this. BBC does not recognize command TIMER that captures time counter that can be used to calculate time elapsed. What is the BBC Basic equivalent?

Thanks
Marty

DEFDBL A
PRINT"NOW COMPUTING"
A=1
Y0=TIMER 'Captures time of beginning
FOR I=1 TO 100000
A=TAN(ATN(EXP(LOG(SQR(A*A)))))+1
NEXT
Y1=TIMER 'Captures time at end
PRINT"SECONDS: ";Y1-Y0 'Displays the difference ie time elapsed
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Computing time elapsed, how?

Post by Richard Russell »

Marty wrote: Fri 04 Oct 2024, 07:31 What is the BBC Basic equivalent?
Here is a close translation of the code you listed into BBC BASIC:

Code: Select all

      PRINT"NOW COMPUTING"
      A=1
      Y0=TIME :REM Captures time of beginning in cs
      FOR I=1 TO 100000
        A=TAN(ATN(EXP(LOG(SQR(A*A)))))+1
      NEXT
      Y1=TIME :REM Captures time at end in cs
      PRINT"SECONDS: ";(Y1-Y0)/100 :REM Displays the time elapsed
On the laptop I am using to write this it displays 0.08, so you might want to increase the loop count (perhaps to 1000000) to get a more consistent measurement.
Marty
Posts: 4
Joined: Fri 04 Oct 2024, 05:13

Re: Computing time elapsed, how?

Post by Marty »

Thank you Richard

Original code from 1987 started at 2500 to accommodated the CPUs of the time.

One more thing if I may, PowerBasic and QBasic happily display the result for several decimal places. BBC truncates to only 2. How can I increase the decimal point range?
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Computing time elapsed, how?

Post by Richard Russell »

Marty wrote: Wed 09 Oct 2024, 10:50 PowerBasic and QBasic happily display the result for several decimal places. BBC truncates to only 2.
It isn't truncating. TIME returns an integer number of centi-seconds so it's inevitable that after division by 100 you get only two decimal places.

To get more you would have to time for longer, so for example if you loop for 10000000 (10 million) and divide by 1000 rather than 100 it will give you three decimal places, but you have a longer wait for the result.

Those BASICs which display several decimal places are lying anyway, none of the Operating Systems on which BBCSDL runs can reliably make time measurements with an accuracy better than 1 millisecond, and even that isn't guaranteed. It's more honest to display only two meaningful digits, than several meaningless digits.

Anyway on a laptop, such as the machine I am typing this on, the CPU clock is dynamically adjusted according to battery state, processing load etc. so a time measurement will be inconsistent (unless you turn those features off in the settings).
Marty
Posts: 4
Joined: Fri 04 Oct 2024, 05:13

Re: Computing time elapsed, how?

Post by Marty »

Ok now I get it about the clock Turns out PowerBasic is also centiseconds so I assume so is QBasic.

There is one other point I would like clarified and that is the accuracy of the mathematical operations in the final value of A, which starts as 1 but is not quite the 10000001 as it should be at he end of the loops.

The program as you know does forward-backward operations on math functions
If one takes TAN of 1 = 0.17455064, then then ATN of the answer should be 1, then 2 3, 4 etc but this is not exactly the case after a large number of loops as below.

We add the line in the code
print"A =";A
after the loops to display the final value of A

Running at 10000000 (10-million) cycles the results

In PowerBasic (running uncompiled, not exe)
5.49 seconds
A=1000001.0013712

In QBasic (running uncompiled )
165.71 sec
10000001.00141461

However in BBC we get
8.68 sec
2.48482277

BBC gives an entirely wrong answer.

Martin
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Computing time elapsed, how?

Post by Richard Russell »

Marty wrote: Fri 11 Oct 2024, 03:40 BBC gives an entirely wrong answer.
I suspect you haven't taken account of the fact that LOG() in BBC BASIC is log-to-base-10 whereas LOG() in QBasic is the natural (Napierian) logarithm. To calculate the natural logarithm in BBC BASIC you need to use LN() (this is much more consistent with the conventional mathematical notation):

The following program:

Code: Select all

      PRINT"NOW COMPUTING"
      A=1
      Y0=TIME :REM Captures time of beginning in cs
      FOR I=1 TO 10000000
        A=TAN(ATN(EXP(LN(SQR(A*A)))))+1
      NEXT
      Y1=TIME :REM Captures time at end in cs
      PRINT"A =";A
      PRINT"SECONDS: ";(Y1-Y0)/100 :REM Displays the time elapsed
gives this result here:

Code: Select all

NOW COMPUTING
A =10000001
SECONDS: 6.47
If I force it to display more digits by adding @% = &1011 I get this:

Code: Select all

A =10000001.00108122
To convert QBasic code into BBC BASIC code you can always use the QB2BBC utility. It's not perfect, but it will translate LOG() correctly as you can see below:

qb2bbc.png
You do not have the required permissions to view the files attached to this post.
Marty
Posts: 4
Joined: Fri 04 Oct 2024, 05:13

Re: Computing time elapsed, how?

Post by Marty »

Thank you Richard

All good now.

Marty