The thing is unless you specialize in optimization, it’s probably just better to leave it to the compiler. You have to really know what you’re doing to have a chance of outsmarting the compiler.
The problem is not efficiency, it's readability and extendability. Why not use arrays and vector operations? And why would you need a specific class for a 4x4 matrix rather than a generic class?
Processors are optimized for matrix*vector multiplications so the ideal solution is to use the instruction for that and just do it four times for the columns of the matrix
I've done the same many times, and a necessity when elements are stored in fields. This produces operations on the stack which is multiple times faster than the heap.
Mine is like this, however I have an extra step where I cache the initial values first to remove some of the lookup time. It does make a difference when you are processing lots of matrices each frame.
does nobody see that he’s allocating a Matrix4x4 pointer but returning it as a copy? Why is every variable stored as a member variable instead of an array?
This is basically the difference between scientist & engineer. (Computer) scientists see this and thing, woah, why is this coded for specific n = 4? I could make this an nxn matrix and solve it with that algorithm i learned in class! Whereas the (software) engineers see this and go "yep, this is how we do it because O(1) is a faster than that O(n^3) general matrix multiplication function. Duh.
Nice of the developer to give the compiler's loop unroller the day off.
This is a efficiency-minded fellow! Does not belong there!
Believe it or not, but this is the fastest way to do it, and any other way is counterproductive, since you need such operations many times per frame.
Yes, and a reasonably smart compiler will vectorize this.
There are faster multiplication algorithms for matrices, e.g.
The fastest way would be to use SIMD but that's definitely much more advanced solution.
This is far from the fastest way. The fastest ways involve:
Surely the compiler can unroll a loop with a constant length though, so writing this code with loops would almost certainly be better.
The thing is unless you specialize in optimization, it’s probably just better to leave it to the compiler. You have to really know what you’re doing to have a chance of outsmarting the compiler.
This presumes your compiler doesn't catch it and do it anyway...
You're gonna wanna check out alphatensor, Deepmind is using AI to find faster ways than this and they're succeeding.
The problem is not efficiency, it's readability and extendability. Why not use arrays and vector operations? And why would you need a specific class for a 4x4 matrix rather than a generic class?
Processors are optimized for matrix*vector multiplications so the ideal solution is to use the instruction for that and just do it four times for the columns of the matrix
Not horror. Very common in game or other performance oriented applications. You'll often special case common scenarios and general case uncommon ones.
There's literally nothing wrong with this
next level loop unrolling
There is a mistake on the cell one up and over from the bottom right. a.M32 should be a.M33
Wonder how long it would have taken OP to find that logic error had you not pointed it out...
Well damn, you’re right - good eye.
No way! That's my wifi password!
How not to lose your job at Twitter.
Eh, it's not too bad. Sure it's a little ugly, but the operation itself is well-understood so it's clear what the code is doing.
Writing this by hand is/would be tedious and highly error-prone.
C# has Spans which are value types (and function similarly to arrays).
Passing objects by value hmmmmm
You can't pass by reference in an operator overload afaik (in C#)
How do you recognize it’s passed by value?
In c# custom object variables are always an implicit pointer so it is effectively a pass by reference
Depends on whether or not Matrix4x4 is a value type.
Now that, you have a point, it ought to be done by reference to speed it up further.
You could just use matlab
I've done the same many times, and a necessity when elements are stored in fields. This produces operations on the stack which is multiple times faster than the heap.
Mine is like this, however I have an extra step where I cache the initial values first to remove some of the lookup time. It does make a difference when you are processing lots of matrices each frame.
If this was done by Copilot, it's probably worth the cut on developer time...
Check out Ejml library in Java if you think this is horror.
I had to do that in a university assignment. Yes we had to do 2x2 matrix multiplication before we learned about loops.
Omg I cannot believe you do this. You're writing code in Java!!
Is this for . Or X multiply? The people need to know
does nobody see that he’s allocating a Matrix4x4 pointer but returning it as a copy? Why is every variable stored as a member variable instead of an array?
The Matrix4x4 class in itself is an insult to any CS class I ever attended
And why exactly is that?
This is basically the difference between scientist & engineer. (Computer) scientists see this and thing, woah, why is this coded for specific n = 4? I could make this an nxn matrix and solve it with that algorithm i learned in class! Whereas the (software) engineers see this and go "yep, this is how we do it because O(1) is a faster than that O(n^3) general matrix multiplication function. Duh.
What is this algorithm for? It's like a bullshit
It's bog-standard
Lmfao learn to code
passing objects by value and allocating dynamically. lmao
This is C#
“Library? What’s a library?”