dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 15 February 2006, 19:18 PM Post subject: |
|
|
| Quote: | | Is there some sort of performance penalty for using a function rather than a subroutine[...]? |
When a function is called, space is reserved on the stack for the return value. This is accomplished using a single instruction (typically ADDSP_B, but more complicated for a string). Then the parameters, if any, are pushed on the stack. When the function returns, the return value is used in other operations or popped off the stack into a variable.
When a subroutine is called, the parameters are pushed on the stack. ByRef parameters always require two bytes of stack space irrespective of the type. ByVal parameters require one to four bytes depending on the type.
Whether the subroutine form or the function form is more time efficient depends on the number and types of the parameters as well as the passing method (ByRef vs. ByVal). The subroutine form will often use less stack space but that calculation also depends on the passing method as described earlier.
The primary advantage to the function form is that it may be used in an expression just as any other value may be. If this capability is not needed, and the minor differences in execution time and stack use are not important then the choice is largely a matter of preference.
As a side note, it may be useful to add the --listing=<filename> option to your project file. Even though the ZX instruction set is not yet documented, the opcode names follow a regular pattern (with some exceptions) and their functions can most likely be deduced from the context by those with a modicum of familiarity with lower level languages and stack-based architectures. Inspection of the generated code often yields useful clues as to the relative performance of alternative code sequences. |
|