Idle of life - idle game based on Conway's Game of Life

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? :slight_smile:
Here are some things I read:

  1. for is faster than for ipairs
  2. Inserting values on an index like ctable[I] = is faster than table.insert method
  3. 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.

  1. I changed for pairs, to regular for
  2. I am inserting by index
  3. 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? :slight_smile: 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

2 Likes

Something to try is to cache the table.insert function lookup:

local insert = table.insert
for k, v in pairs(cell_seed) do
	insert(ctable, {x = v.x, y = v.y})
end

Another thing which I can’t remember if it is faster or not is to use ipairs instead of pairs.

1 Like

Hi,
I tried your approach.

ctable = {}
local insert =  table.insert

stopwatch.start("seed_britzl")
for k, v in ipairs(cell_seed) do
	insert(ctable, {x = v.x, y = v.y})
end
print("seed_britzl " .. stopwatch.stop("seed_britzl"))

Result is:
image

I don’t know if there is some disadvantage in running all 3 approaches one after another. But my for is still faster. It would be good if someone try this too. To have some comparison.

I don’t think it will be a problem.

Yes, you optimised solution is still much faster. But it’s good to know that it helps to cache functions when doing loops over many items.

I agree. Especially in games, where every millisecond counts.

1 Like

Hello,
so I changed GoL for loop into my fast solution. So now I am using indexes, cell table where are 2 tables for cells x and y.

I can’t believe it. So I again tried 250 000 cells on 1 000 x 1 000 board. At the start of this performance madness. I hit ctrl + b and what I saw. I still don’t believe it right now so I will do a lot of tests. But there is a log, of time to apply GoL logic per cell.

image
Yes, I added index count into a log, which may cause a little bit worse times. But those numbers? I don’t know if this can be real. From 0.04 to 0.002 most times?

Thank you much for your time and attention.
Again I appreciate every thought from you!

2 Likes

Hello,
I am sorry, I didn’t make any progress now. But I have some thoughts to share with you.

So first of all. I decided to bring this game to the store. Finish all things that I wanna include, I have gained some dedication thanks to these dev diaries. Also probably try something like AdMob in the future, I am not expecting millions of downloads and thousands of dollars on my account :grinning_face_with_smiling_eyes:. But I wanna try it.

So currently I also decided, that I will game make open source. So I have prepared a Github repo. But I don’t know anything about open source licensing. Only that there are few like Apache, MIT, and GPL. But which will describe better my situation I still don’t know. I am currently investigating it. So when I chose. I will post the link on the repo.

Thank you much for your time and attention.
Again I appreciate every thought from you (Now especially about licensing)!

1 Like