I'm trying to implement simple software 3D engine. So I need a matrix 4x4 to represents transformations. Each cell type of my matrix is in the System.Double format which means entire matrix will use 8 bytes * 16 cells = 128 bytes. Currently I have implemented this as a class. Also this matrix is an immutable type.
So now, when I trying to multiply vector by this matrix, I'm doing something like this: matrix.m11 * vector.X (etc...). As I think, to access field m11, program first need to get 'matrix' reference value, then check it for null, then find m11 value, yes? If I would change class to struct, my multiplication coukd be alot faster? I know 128 bytes struct is big, but if I write method that works only using ref/out keyowrds? It will be good chooose?
As I see in SharpDX library, matrix is a struct. Should I change class to struct then to improve performance?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I would not recommend to use class for storing a double4x4 matrix for a 3D engine for the following reasons:
fixed
instruction in C#. It also applies to interop to other libraries (a physic engine will expect a float4x4 struct and never an object instance)On a side note, if you care about performance:
ref
instead of by copy on all your matrix calculations.