|★ APPLICATIONS ★ UTILITAIRES RSX/LIGNE DE COMMANDE ★ Profiler ★|
|Profiler|Computing With the Amstrad)||Applications Utilitaires Rsx/ligne De Commande|
JOHN RAWLINSON offers a neat way of shifting into top gear on your CPC
SOONER or later one problem that faces everyone who writes computer programs is how to make a program run faster. Obvious solutions are to rewrite the program in machine code, or use a compiler to do this for you. However 90 per cent of the time a program takes to execute will usually be spent in a small section.
Rewriting this part of the program in a more efficient way will often give the required improvement in speed. This leaves the problem of finding where a program spends most of its time. The easy way to do this is with a program called a profiler and this article describes one which can be used with Basic programs on the CPC range.
It comes in two parts, a small machine code program which sets up two new RSX bar commands, and a chunk of Basic which is added to the program being profiled in order to display the results.
Program I enables a fast ticker -1/300th of a second - event. It looks for the line the Basic interpreter is currently handling 300 times a second and increments a two byte count for that particular line number.
The machine code for this program is contained in the data statements. The program should be typed in and then saved. When it is run two new bar commands will be set up - |START and |STOP. |START clears the count for all lines to zero and sets off the fast ticker event, while |STOP switches off the event and halts the line incrementing.
The code works by looking at a Basic interpreter variable - stored in location &AE36 on a 464, or &AE1D on a 664 or 6128 - which contains the start address of the current Basic line. It is then fairly simple to look a couple of bytes further along the line and discover its number.
Program II is the Basic section of the profiler, and it should be typed in and saved in Ascii form using:
The procedure for profiling a program, is as follows. Firstly if the one you wish to profile contains a SYMBOL AFTER command REM it out along with any MEMORY command that attempts to raise HIMEM above that set by Program I. Then in direct mode type in the SYMBOL AFTER command you have just removed and run Program I.
Next the program to be profiled must be loaded and renumbered if necessary - (see later) and then Program II MERGEd with it. A |START command should then be added to the beginning and a |STOP command entered at the point where it finishes.
Inserting the |STOP command is not too critical as the program can be halted by pressing Escape before this command is met.
Finally run the main program and when the |STOP takes effect or you press Escape type RUN 30000 and this will execute the results section of the profiler.
The first thing you will see is a menu allowing you to select one of three options - a display of the same information either by individual line numbers, groups of 10 line numbers or groups of 100.
The information presented is the number of ticks for that line or group and the percentage of the total time spent executing the program that was expended on the line or group. A tick is recorded each time the line is seen by an event.
As set up the program can only handle line numbers in the range 0 to 1000 which means that your program must be renumbered to fit into this range before the profiler is used on it.
The variable nl in line 30060 sets the number of lines the profiler displays results for. This should be changed to a value between 100 and 1000 in steps of 100 depending on the size of your program.
Keeping it small will make the analysis process quicker. The use of the fast ticker event means that the profiler will spot all but the simplest lines.
However it also means that the two byte count may overflow if the profiling session lasts more than 30 minutes. The programs time range can be extended by using the 50Hz frame sync, event instead. This can be done by making the following changes to Program I:
90 IF i<>23438 THEN PRINT"Check sue error !"':END
When all the results have been displayed pressing any key will return you to the menu.
By using the profiler line and group options you should be able to locate where any program spends most of its time. Having done this the normal techniques for speeding up a program can be applied.
The most common of these is to remove as much computational work as possible from inside loops. Although this may sound obvious most improvements in program speed come from just this. For example, consider this fragment of Basic:
10 FOR I=1 TO 100
This could be made faster byplacing the multiplication by 0.1 outside the FOR loop as follows:
10 FOR I=1 TO 100
Other standard tricks to speed up programs are to use integer variables whenever possible since these can be manipulated much faster than floating points, and to calculate things in advance - if possible.
'Other standard tricks to speed up programs are to use integer variables .. '
For instance, if you want to draw a circle on the screen you may well end up using the SIN and COS functions. However a quicker way is to store all the sines and cosines required in an array and use this to supply the values when the circle is drawn. Such an array is called a look-up table. Although the Basic interpreter on the Amstrad is fast there is a rather surprising bottleneck which makes the execution of many programs much slower. This is because the operating system prints characters on the screen painfully slowly.
The only way around this is to buy one of the Basic extension programs which contain a new routine for doing the job, a typical example being Beebugsoft's Toolkit ROM.
Don't gain the impression that moving things out of all loops will improve the speed of programs. If a loop is only executed a few times no amount of work on it will speed it up.
The loops to concentrate on are those that are executed the most number of times - the so-called inner loops of the program. Using the profiler presented here will help to locate these, and hence should help you to increase your running speeds by a fair amount.