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
Computing time elapsed, how?
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32
Re: Computing time elapsed, how?
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
-
- Posts: 4
- Joined: Fri 04 Oct 2024, 05:13
Re: Computing time elapsed, how?
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?
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?
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32
Re: Computing time elapsed, how?
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).
-
- Posts: 4
- Joined: Fri 04 Oct 2024, 05:13
Re: Computing time elapsed, how?
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
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
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32
Re: Computing time elapsed, how?
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
Code: Select all
NOW COMPUTING
A =10000001
SECONDS: 6.47
Code: Select all
A =10000001.00108122
You do not have the required permissions to view the files attached to this post.
-
- Posts: 4
- Joined: Fri 04 Oct 2024, 05:13
Re: Computing time elapsed, how?
Thank you Richard
All good now.
Marty
All good now.
Marty