Quark wrote on Nov 1st, 2009, 5:06am:The trick here in terms of performance is not to create arrays or copy arrays in the draw() function.
The other tricks are to not use 2D arrays, and to minimise the number of element accesses...
You don't need to count 8 neighbouring cells for each cell as you go along, because neighbouring cells have overlapping sets of neighbours.
I came up with a neat trick whereby you only need to look at 3 new cells for each cell calculation (instead of 9!)... well, except for the cells along the left edge of the grid (realisation: if you have many columns, the extra overhead for just the first column tends towards zero as an average for all columns, and hence all cells).
Conway_mod_by_spxl_1_3Note: this program also does something a little different in that it doesn't calculate a futureGrid, as it were, but instead a "what are the changes grid"; I supposed that drawing the cells (plotting pixels) is slow, so instead of drawing the entire grid for each iteration, I only plot the births and deaths. This also allows for an interesting feature of plotting different colours to see a kind of history of the lives (and deaths) of the cells.
By the timing tests I'd run, this version runs nearly two and a half times as fast as the original that comes as a sample with the Processing environment, and represents the most advanced version of Life I've ever written. Having said that, it is still childs play compared to serious Life optimisations.
Check out a program called
golly... you can run simulations thousands (or millions!) of cells wide and high at insane realtime speeds.