I’m currently working on a module that offers a lot of utilities when working with Matrices, all good so far, but I just wanted to get people’s opinion on how efficient the code should be. I was testing out a bit what I got so far, to see if the Matrix.new() constructor that I made for example, is being a bit heavy in terms of micro optimization. So I compared it with CFrame.new() and assigning a table to see if how much time each task takes.
local Matrix = require(game.ReplicatedStorage.ModuleScript)
local start = tick()
local A = Matrix.new({2, 4}, {2,55}, {7,5}, {2,2}, {2, -5})
print(tick() - start, " Matrix.new()")
local start = tick()
local cf = CFrame.new(2,2,2)
print(tick() - start, " CFrame.new()")
local start = tick()
local t = {}
print(tick() - start, " Table")
And these are the results that I got on average
And of course this isn’t always precise, because sometimes I get really random and suprising results
So again, my question is, is this good enough, or does it take too much to execute? And a bonus question is, how important is a module’s efficiency? Thank you!
0.9 - 37 microseconds (10^-6 s) isn’t bad, so this isn’t taking too long to execute. A modules efficiency is important because you want the most productivity you can get from it, whilst using the least resources (such as memory and speed). Using a lot of resources will ruin performance
Thank goodness I read this, or else I couldn’t help you:
Judging on the amount of time, that is A LOT of time wasted. Unless, of course, your computer is sloe. For example, in the thread I sent you, they set the CFrame as well as many other properties 100 times over and got a result of 0.005 seconds!
I think efficiency is a high priority, no matter what it is; no one wants to play a game that their device cannot run on.
Thank you for the help! I have no idea how creating 100 cframes just took 0.005 for them, because as I show in the examples above, creating one cframe for me took way more, perhaps it is indeed something with my computer.
That post is made before the Luau update. The new VM drastically increased the speed of function calls.
Also your module looks pretty fast. No need to optimize it more.
If you really want to micro-optimize you could remove the unnecessary tables you’re creating since they fill up a small amount of memory that you may want for whatever reason. This of course only works if all of the matrices are always the assumed dimensions.
The e-6 means it actually took 0.000005, so that’s consistent with the results for 100 CFrames. Here’s a link that explains scientific notation better than I can: Scientific notation - Wikipedia
Edit: 100 CFrames would probably be closer to 0.0005, so I guess Luau is considerably faster.
Ty dude! I totally forgot about Luau, at first I was scared beacuse I though it took too much time. Glad to hear that it’s fine!
Also, since you mentioned that in the example I’m assigning a lot of table along with calling the constructor as well, wanted to point out that I wanted to make this module as creative as possible, so I made the Matrix.new() constructor accept different formats of inputs:
The format you see in this question, the one with a lot of tables, where each table is a row vector
The ability to use Vector3s and Vector2s as well as row vectors, of course as long as the matrix’s is nx3 (for Vector3s) or nx2 (for Vector2s)
It can take a bunch of number values put next to each other, where each number is an entry from the matrix, put in order
It can also accept a string, formated this way (for example a 3x3 matrix would be written this way) "entry, entry, entry; entry, entry, entry; entry, entry, entry"
Based on your response, it sounded like you were misinterpreting what the numbers actually were. Sorry if my response seemed unnecessary.
(These responses were what made me think that)
On an unrelated note, you might get more accurate results if you repeat the operation more than just once, (e.g. a couple thousand times) so that you can get a more accurate average.
Yeah my wording might’ve been wrong a bit, I didn’t mention the unit (didn’t say 0.005 seconds). I did try tens of times! And yeah I got really jumpy results, but occasionally it got there. Thank you!