Hi-Po dynamic Matrix tool, should I use a Class or a Struct for this? (VB.NET)

Viewed 20

I made a Class for matrix operations, [MatrixObject]. I am mainly concerned with the inverse; I need to calculate many large matrix inverse's for this application.( size:~[R100xC100])

Right now the performance for computing the inverse is:

  • size:50x50 ---- time:~200ms
  • size:100x100 - time:~2.3s
  • size:200x200 - time:~29s

That sounds slow but its not, try beating that without purchasing libraries, its hard...

I can probably squeeze out a tiny bit more speed by cleaning the loops... But I'm wondering if turning this into a Structure would have major performance gains and makes sense ('would a smarter better looking programmer do this?').

The speed of this inverse calculation is essential to the application but it contains a large dynamic array. The internet says large arrays are a no go for Structs... But are there circumstances where you would use a Struct anyway? Also, what happens if the Struct w/ methods is wrapped in Class?

My knowledge of stack and heap is to the extent of stuff said in youtube videos. I don't have a deep enough understanding to answer this question.

Here as example of the Code:

As a Class (current version)

' Every thing inside a Class ----------------
Public Class MatrixObject1

    Private Structure Matrix4F_Array
        Dim A() As Matrix4x4
    End Structure

    Private m_ROW_Arrays() As Matrix4F_Array    '<-- array of [Matrix4x4] arrays 
    '
    '   m_ROW_Arrays(0)     .A(0)   .A(1)   .A(3)    ...
    '                       M11     M12     M13   
    '   m_ROW_Arrays(1)     .A(0)   .A(1)   .A(3)    ...
    '                       M21     M22     M23 
    '   m_ROW_Arrays(2)     .A(0)   .A(1)   .A(3)    ...
    '         :             M31     M32     M33 
    '         :

    Public Sub New()
        '...
    End Sub

    Public Shared Function Multiply(ByRef Mat1 As MatrixObject1, ByRef Mat2 As MatrixObject1) As MatrixObject1
        ' ....
    End Function

    Public Shared Function Inverse(ByRef i_Mat As MatrixObject1, ByRef success As Boolean) As MatrixObject1
        ' ....
    End Function

    ' * also includes propertys for size and shared operators: *,+,-
    '   and misc. functions.

End Class

As a Structure w/methods, and wrapper Class (for non essiential/slow methods); what i'm considering doing

' Structure with only essential, tight functions
Public Structure MatrixStruct

    Private Structure Matrix4F_Array
        Dim A() As Matrix4x4
    End Structure

    Private m_ROW_Arrays() As Matrix4F_Array    '<-- array of [Matrix4x4] arrays 

    Public Shared Function Multiply(ByRef Mat1 As MatrixStruct, ByRef Mat2 As MatrixStruct) As MatrixStruct
        ' ....
    End Function

    Public Shared Function Inverse(ByRef i_Mat As MatrixStruct, ByRef success As Boolean) As MatrixStruct
        ' ....
    End Function

End Structure


' 
' Matrix Class that inlcudes [MatrixStruct] member
'   -Class Methods have error checks and other misc. stuff
Public Class MatrixObject2

    Private m_Struct As MatrixStruct

    Public Sub New()
        '...
    End Sub

    Public Shared Function Multiply(ByRef Mat1 As MatrixObject2, ByRef Mat2 As MatrixObject2) As MatrixObject2
        ' ....
        Dim ret As New MatrixObject2
        ret.m_Struct = MatrixStruct.Multiply(Mat1.m_Struct, Mat2.m_Struct)
        Return ret
    End Function

    '   :
    '   :
    '   :
    ' * also includes propertys for size and shared operators: *,+,-
    '   and misc. functions.
End Class
0 Answers
Related