Hello,
so next thing I found in my performance journey. Method table.insert()
is slower than adding by index, ok little joke I was thinking it, the same thing I had with Linq in C#, it makes things easier to write but slower to process.
So thanks to my random cell seeder I made a little test. 1 000 000 cells. From one table to another.
Two different approaches
So the first case is my old code. Siple for, to recreate vmath.vector3 to my cell object.
ctable = {}
stopwatch.start("seed_one")
for k, v in pairs(cell_seed) do
table.insert(ctable, {x = v.x, y = v.y})
end
print("seed_one " .. stopwatch.stop("seed_one"))
Nothing fancy eh. But itās work. Again thanks @britzl for the awesome stopwatch method.
For million cells, the result is a nice 0.350 everyone now is WOW.
So wanna see something? Something maybe fancy?
Here are some things I read:
-
for
is faster thanfor ipairs
- Inserting values on an index like
ctable[I] =
is faster thantable.insert method
- Rethinking table structure can actually make working with it faster
Okay now. How to change the first approach into a new faster way?
ctable = {
x = {},
y = {}
}
stopwatch.start("seed_two")
for i = 1, 1000000 do
local c = cell_seed[i]
ctable.x[i] = c.x
ctable.y[i] = c.y
end
print("seed_two " .. stopwatch.stop("seed_two"))
So this is it. All 3 principles I found.
- I changed
for pairs
, to regularfor
- I am inserting by
index
- Because all data in the table will be the same, I make little change, so in the table are now only two āarrayā tables x and y. And under the same index, you find cell.
I know what you are thinking. So how well does it perform? Drums, please!
Unbelievable 0.024
So little changes. And I think big progress.
Now just adapt actual code for GoL logic and rendering cells for the new table structure and we will see, how it will perform and what to change next.
Thank you much for your time and attention.
Again I appreciate every thought and idea from you! If you have anything, just say it.
EDIT1: Btw I know it is dumb, random seeder is creating vector3
why donāt create cells, etc. So seeder is actually from the total beginning of the project, where every cell was represented by vector3