Using Global Variables with the Object Model


Getting objects through a lot of intermediate classes tends to be slower than assigning the intermediate classes to global variables. For example, these are equivalent statements in result:

	x = obj.Kinematics.Global.Parameters( "posx" ).Value
	y = obj.Kinematics.Global.Parameters( "posy" ).Value
	z = obj.Kinematics.Global.Parameters( "posz" ).Value

	set gk = obj.Kinematics.Global
	x = gk.Parameters( "posx" ).Value
	y = gk.Parameters( "posy" ).Value
	z = gk.Parameters( "posz" ).Value

While both of these snippets access the x-position of the same object, the first version has to call the same functions three times, whereas the second version reuses the initial object. Let's run that down a bit: in order to get at the x, y and z positions for the object, the following property calls are made:

  1. The Kinematics (X3DObject) property returns a Kinematics object.

  2. The Global (Kinematics) * property returns a KinematicState object.

  3. From the KinematicState object, the Parameters (ProjectItem) * property returns a ParameterCollection object.

  4. The Item (ParameterCollection) * property returns a Parameter object.

  5. The Value (Parameter) * property returns the position.

By using a pointer to the KinematicState object, we save four property calls in our script, as we can just reference the created objects in memory. In short scripts the savings aren't as apparent, but you will really notice the performance improvement when you replace a lot of repetitive calls.