Basic programs, which are all I can write, sometimes run more
slowly than I would like, so I try to incorporate any time-saving
devices I can. Often there is more than one way to achieve the same
effect-for example, GR.0 ? "ã"
and ? CHR$(125) all clear the screen. This simple timing routine will
enable you to find out which of the routines you can use is faster and
whether there is any saving or penalty in memory usage. It uses a
FOR-NEXT loop to execute your piece of code 1000 times but if that is
too long, change the value of R in line 3000.
|
|
|
The first RUN is a setting up run to isolate the effects of the
program itself. Firstly enter a number of REM lines from line 20
onwards equivalent to the number of lines in the routine you are going
to test as you are not testing the time taken to read line numbers.
Next DIMension and initialise any strings or arrays from line 2000 on.
Now RUN the program for the first time.
When the program asks you to change Line 1000, use the cursor up,
enter the new line and enter your first routine for testing, starting
from line 20. RUN the program and record the time and memory readings.
Now change the routine to the one you want to compare and RUN again.
Note the new readings and so on.
I originally wrote the routine to help with a program which used
one long string to contain names and data for several individuals.
Each name was allocated a 20 character section of string with blank
characters (CHR$(32)) being used to extend names to the required
length. This meant that where I wished the program to ask a question
involving an individual's name, there was a variable length space
before the next word in the question. One way round this would have
been to use a FOR-NEXT loop to print the name one letter at a time,
stopping at an end of name flag such as '*'. This seemed a little
clumsy and slow. What I wanted was a character that would neither
print anything on the screen nor move the cursor on. I found five: CHR$(158)
- CLEAR TAB, CHR$(159) - SET TAB, CHR$(253) - SOUND BUZZER, CHR$(254)
- DELETE CHARACTER, CHR$(255) - INSERT SPACE. The buzzer was obviously
out - too noisy! Inserting or deleting characters would involve moving
a number of characters (all blank) along the line which looked time
consuming. However, it was still worth testing.
I first of all entered line 2000 DIM NAME$(20):NAME$= "ERMINTRUDE
". This has ten letters and ten spaces. Then at line 20 ? NAME$;.
Having RUN the program and entered line 1000 X=2728:M1=12370, I
substituted CHR$(157) for each blank and re-ran. When I had timed each
of the four characters I ran the FOR-NEXT loop with an end of name
flag and "ERMINTRUDE" with no subsequent blanks for good
measure. The initial run of 1000 repetitions took 54.56 secs. The
other times were
CHR$
|
TIME
|
BYTES
|
158
|
-14.00 sec
|
0
|
159
|
-14.18 sec
|
0
|
254
|
137.58 sec
|
0
|
255
|
140.80 sec
|
0
|
FOR-NEXT loop
|
101.00 sec
|
76
|
“ERMINTRUDE”
|
-26.00 sec
|
-10
|
The saving in times, which came as a complete surprise
to me, can only partly be explained by the extra lines and scrolling
involved by printing the blanks. Ten CHR$(158) still print 0.005 seconds
faster than ten CHR$(32). What surprising facts can you come up with?
top