$IF 0
CODEPTR.BAS CODEPTR.BAS
Code Pointer Demonstration
Written by Jamshid Khoshrangi
PURPOSE:
This program demonstrates the power of PowerBASIC v3.2's new
code pointers. In this case "power" is defined as "speed"
and for the purposes of this demonstration, code pointers are
compared to a SELECT CASE statement, and both are timed within
a loop.
BACKGROUND NOTES:
This demonstration illustrates the use of a code pointer table
as a replacement for the traditionally utilized SELECT CASE
statement under special conditions. Those conditions? Well,
if all the SELECT CASE structure does is route the program to
one of a selection of SUBs, and each of those SUBs accepts the
same parameters, or no parameters at all, such as:
SELECT CASE x
CASE 1
DoTaskOne
CASE 2
DoTaskTwo
CASE 3
DoTaskThree
END SELECT
This type of SELECT CASE is common in finite state systems. I
can personally see great use for this in two of my own programs.
Even if you don't program this way now, after you see the speed
gains presented by code pointers under these conditions, you
may start asking yourself just how you can take advantage of the
code pointer table method!
If you'd like more information on how this can be used in real
programs, feel free to contact me on the POWERBASIC echo in
Fidonet.
Jamshid Khoshrangi
$ENDIF
' This statement REALLY SPEEDS THINGS UP!
$ERROR ALL OFF
$DIM ALL
DEFINT A-Z
DIM CodePtrTable (1:8) AS SHARED DWORD
' This type is just used for this demo... no real meaning
TYPE OurType
Alpha AS INTEGER
Beta AS STRING * 32
Gamma AS BYTE
Delta AS LONG
END TYPE
' It is important to note that each of the following SUB's that
' will be in the CodePtrTable accepts the same number and types
' of parameters!
DECLARE SUB A (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB B (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB C (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB D (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB E (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB F (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB G (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB H (One AS INTEGER, Two AS STRING, Three AS OurType)
DECLARE SUB DoTheDemo ()
DECLARE SUB InitializeTheCodePtrTable ()
DECLARE SUB RunTheSelectCaseDemo ()
DECLARE SUB RunTheCodePtrDemo ()
' Demo begins here!
DoTheDemo
END
SUB DoTheDemo ()
DIM SelectCaseEndTimer AS LONG
DIM CodePtrEndTimer AS LONG
CLS
VIEW TEXT (25,5)-(70,20)
PRINT "Code Pointer Demonstration"
PRINT
InitializeTheCodePtrTable
MTIMER
RunTheSelectCaseDemo
SelectCaseEndTimer = MTIMER
PRINT "SELECT CASE: ", SelectCaseEndTimer
MTIMER
RunTheCodePtrDemo
CodePtrEndTimer = MTIMER
PRINT "CODE POINTER: ", CodePtrEndTimer
PRINT
PRINT "Approximately";
PRINT INT(1/(CodePtrEndTimer / SelectCaseEndTimer)*10)/10;
PRINT "times faster!"
PRINT
PRINT "Need I say more?"
PRINT "Kudos to PowerBASIC version 3.2!"
END SUB
SUB InitializeTheCodePtrTable ()
' The table must be initialized!
CodePtrTable(1) = CODEPTR32(A) : CodePtrTable(2) = CODEPTR32(B)
CodePtrTable(3) = CODEPTR32(C) : CodePtrTable(4) = CODEPTR32(D)
CodePtrTable(5) = CODEPTR32(E) : CodePtrTable(6) = CODEPTR32(F)
CodePtrTable(7) = CODEPTR32(G) : CodePtrTable(8) = CODEPTR32(H)
END SUB
>>> Continued to next message
* OLX 2.1 TD * Ask me about finite state automata
--- Maximus/2 2.01wb
---------------
* Origin: Sound Stage BBS - Live Via Satellite - (604)944-6476 (1:153/7070)
|