Monthly Archives: July 2017
BASIC Code to calculate a programs elapsed wallclock time
Calculating the elapsed wall clock time of a program is helpful to determine how much time it actually takes for a program to complete. The easy method is to take the time the program ends and then subtract the time the program starts. In all multivalue BASIC languages you can use the TIME() function to return the number of seconds from midnight. The problem with this solution is if you have to calculate the elapsed time through midnight.
If you start at 11:59pm and complete at 12:01am the internal times would be 86340 for 11:59pm and 60 for 12:01am. So using the above calculation of end time minus start time which would be 60 – 86340 = -86280. This elapsed time does not make any sense at all.
To correct for the midnight crossing we need to use the date the program started and the date the program ended. One thing we need to know is that there are 86400 seconds in a day. With that in mind the new calculation would be ((Date End – Date Start) * 86400) + (Time End – Time Start). This formula will provide a more accurate method of calculating elapsed time if there is a midnight crossing.
The internal dates are calculated using the DATE() function that calculates the number of days from the Epoch. Multivalue databases use the Epoch 12-31-1967. The Epoch is where the internal date is zero.
Using the above example, lets add that the program started on 01-01-2017 and it was completed on 01-02-2017.The internal date for 01-01-2017 is 17899 and the internal date for 01-02-2017 is 17900, the calculation now looks like the following.
((17900 – 17899) * 86400) + (60 – 86340) which gives (86400 – 86280) that equals 120 seconds or 2 minutes which is the correct elapsed time. Some, but not all multivalue database system provide for a date and time @-variable that define the program start date and time. You may see that there is a tiny glitch in the logic above in that if I get the time that program start and the date changes before the value of the date is read the calculation will be off. The converse of this glitch would also be true if I get the date and the time crosses midnight. The @-variables help prevent this glitch.
So, in my programs when I wish to calculate the elapsed wall clock time of my program in a midnight or date crossing condition, I create an equate that provides the elapsed time.
EQUATE ELAPSED LITERALLY “((DATE()-@DATE)*86400)+(TIME()-@TIME)”
At the end of my program I use the following.
PRINT “Elapsed: “:OCONV(ELAPSED,”MTS”)
This only provides the elapsed wall clock time of the BASIC program. You may want to also determine the amount of CPU time spend on executing the program. The wall clock time of a program can be influenced by the load on the system.