Efficient Use of Subroutines and External Functions
See also the topic Subroutines.
Internal Subroutines
The overhead of calling an internal subroutine is very small. This is because all parameter passing is via global variables. For a small subroutine that is used in more than one program, consider using the INCLUDE statement to add it to the main program as an internal subroutine.
Note
Because all identifiers used in a module are global in scope, you will need to be careful that internal subroutines included in this way do not use the same identifiers as the programs that call them.
External Subroutines and Functions
External subroutines and functions are started in a similar way to cataloged DataBasic programs and therefore have the same performance overheads. However, the memory structures required by each user to run the routine are cached, and these overheads are therefore only incurred the first time each user calls the routine. Subsequent calls use the copy in the cache. The size of the cache has been chosen to balance program efficiency and overall system performance; it favours the use of a small number of frequently called routines. If your application uses a large number of external subroutines and/or functions, a larger cache might make it more efficient; contact NEC Software Solutions for advice.
The following points should be considered when using external subroutines and functions if performance is or may be a problem:
-
Consider passing data in COMMON rather than as parameters. This is much more efficient, as there is no overhead in passing COMMON parameters, whereas an argument list requires parameters to be copied.
-
Avoid opening files in an external routine as, unless the file variable is passed back to the calling programs, this will result in the file being closed on exit from the routine (see topic File Open And Close). Create your file variables in COMMON and use the COMMON variables to access the opened files from within your external routines.
Note
If you create your file variables in a named COMMON section, your program must close the files concerned on exit. If this is not done, the files will remain open until the user logs off.
-
Although it is not necessary to catalog a program that calls an external routine, it is much more efficient if you do. If a number of users are running the same cataloged program, they all share the same copy of the program object code. If the program is not cataloged, they each have their own copy in memory.