PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
UDTs and speeddifference.-VBForums
Results 1 to 4 of 4

Thread: UDTs and speeddifference.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    UDTs and speeddifference.

    A rather weird thing.

    Two versions of a function:
    VB Code:
    1. Public Function quatRotateVector(q As tQuaternion, v As tVector) As tVector
    2. Dim qv As tQuaternion
    3. Dim qc As tQuaternion
    4. Dim qt As tQuaternion
    5.    
    6.     'qv.w = 0#
    7.     qv.X = v.dX 'first converting the tvector to a tquaternion
    8.     qv.Y = v.dY
    9.     qv.z = v.dZ
    10.    
    11.     'qv = quatMultiply(quatMultiply(q, qv), quatConjugate(q))
    12.     'qc = quatConjugate(q)
    13.     qc.w = q.w
    14.     qc.X = -q.X
    15.     qc.Y = -q.Y
    16.     qc.z = -q.z
    17.     'qv = quatMultiply(q, qv)
    18.     qt.w = q.w * qv.w - q.X * qv.X - q.Y * qv.Y - q.z * qv.z 'then using the tquaternion in the calculations
    19.     qt.X = q.w * qv.X + q.X * qv.w + q.Y * qv.z - q.z * qv.Y
    20.     qt.Y = q.w * qv.Y - q.X * qv.z + q.Y * qv.w + q.z * qv.X
    21.     qt.z = q.w * qv.z + q.X * qv.Y - q.Y * qv.X + q.z * qv.w
    22.    
    23.     'qv = quatMultiply(qv, qc)
    24.     'w = qt.w * qc.w - qt.X * qc.X - qt.Y * qc.Y - qt.z * qc.z
    25.     quatRotateVector.dX = qt.w * qc.X + qt.X * qc.w + qt.Y * qc.z - qt.z * qc.Y
    26.     quatRotateVector.dY = qt.w * qc.Y - qt.X * qc.z + qt.Y * qc.w + qt.z * qc.X
    27.     quatRotateVector.dZ = qt.w * qc.z + qt.X * qc.Y - qt.Y * qc.X + qt.z * qc.w
    28. End Function
    And:
    VB Code:
    1. Public Function quatRotateVector(q As tQuaternion, v As tVector) As tVector
    2. Dim qv As tQuaternion
    3. Dim qc As tQuaternion
    4. Dim qt As tQuaternion
    5. Dim w As Double
    6.    
    7.     'qv = quatMultiply(quatMultiply(q, qv), quatConjugate(q))
    8.     'qc = quatConjugate(q)
    9.     qc.w = q.w
    10.     qc.X = -q.X
    11.     qc.Y = -q.Y
    12.     qc.z = -q.z
    13.     'qv = quatMultiply(q, qv)
    14.  
    15.     qt.w = q.w * w - q.X * v.dX - q.Y * v.dY - q.z * v.dZ 'use the tvector directly in the calculations
    16.     qt.X = q.w * v.dX + q.X * w + q.Y * v.dZ - q.z * v.dY
    17.     qt.Y = q.w * v.dY - q.X * v.dZ + q.Y * w + q.z * v.dX
    18.     qt.z = q.w * v.dZ + q.X * v.dY - q.Y * v.dX + q.z * w
    19.  
    20.     'qv = quatMultiply(qv, qc)
    21.     'w = qt.w * qc.w - qt.X * qc.X - qt.Y * qc.Y - qt.z * qc.z
    22.     quatRotateVector.dX = qt.w * qc.X + qt.X * qc.w + qt.Y * qc.z - qt.z * qc.Y
    23.     quatRotateVector.dY = qt.w * qc.Y - qt.X * qc.z + qt.Y * qc.w + qt.z * qc.X
    24.     quatRotateVector.dZ = qt.w * qc.z + qt.X * qc.Y - qt.Y * qc.X + qt.z * qc.w
    25. End Function
    They do the same except the former (1.3us) is faster than the latter (1.6us).

    In the former I write the values of the tVector into a tQuaternion and then calculate using the tVector.
    In the latter I calculate directly with the tVector.

    The UDTs hold 4 and 3 Doubles.

    The difference is the same if I put v in a locally declared tVector and use that for the calculations.

    What could cause this difference in speed?

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: UDTs and speeddifference.

    1 - w is equal to zero.
    2 - Your routine doesn't have to get a pointer to a value.

    It would help more to see your UDT's...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: UDTs and speeddifference.

    The UDTs:
    VB Code:
    1. Public Type tQuaternion
    2.     w As Double
    3.     x As Double
    4.     y As Double
    5.     z As Double
    6. End Type
    7.  
    8. Public Type tVector
    9.     dx As Double
    10.     dy As Double
    11.     dz As Double
    12. End Type
    1 - w is equal to zero.
    Would that matter?
    The w component of the tQuaternion based on the tVector also is 0.
    Using it would add some extra work but it would do so in both cases.

    Would it matter where w, v and qv are located?
    2 - Your routine doesn't have to get a pointer to a value.
    Do you mean having the tVector being passed ByRef?

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: UDTs and speeddifference.

    No, w is directly addressed while q.w has to get the address of q first then add the offset of w to get the value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width