PureBasic and the Object-Oriented Programming

Appendix


Optimisations

The paragraphs which follow deal with considerations on our Object-oriented approach to improve the program runtime the performances of .

Optimisation: Get() and Set() methods:
By making frequent calls to Get() and Set() methods, it means many function calls and a loss of performance.
For those who are in search of performance, there are two possibilities to accelerate the process:

Both consist in coupling a pointer to the object, the second solution brings a layer to the first one.

First solution:
The pointer is specified by the Class Structure.
So, for an object Rect from the Rectangle1 Class, write:

Rect.Rect = New_Rect()
*Rect.Rectangle1 = Rect

To act on var2 attribute write:

*Rect\var2

It is then possible both to examine and to modify it.
This is the more simple solution to implement.

Second solution:
The first solution asks to work with two different typed elements: Rect and *Rect.
This second solution, suggests grouping these two elements in a StructureUnion block.

Structure Rect_
StructureUnion
Mthd.Rect
*Mbers.Rectangle1
EndStructureUnion
EndStructure

Creating an object from Class Rectangle1, means declaring the object thanks to this new Structure.
By adapting the constructor, like this:

New_Rect(@Rect.Rect_)

with,

Procedure New_Rect(*Instance.Rect_)
*Rect = AllocateMemory(SizeOf(Rectangle2))
Init_Rect1(*Rect)
Init_Rect2(*Rect)
*Instance\Mthd = *Rect
EndProcedure

To access to the Draw() method, write:

Rect\Mthd\Draw()

To access to the var2 attribute, write:

Rect\Mbers\var2

This second solution has the advantage to have only a single element that can be deal as an object from which all the attributes are accessible from outside of the class.
It preserves also object-oriented notation, although it presents a supplementary level of fields.

The inconvenience concerns essentially the fact that it is necessary to maintain a new structure within the Class.

Contents

[1-2-3-4-5-6-7-8-9]

Top of the page