I started out with a simple implementation:
- A LifeBoard class
- you tell it the size of the game board
- you call "tick" to advance to the next step in the game
- A LifeView class
- it creates a UIView per cell in the board
- you call "changeViewBoard" to update every view to it's new state
Super simple. And it worked -- really slowly. So, out came instruments. And, I found the following:
- [UIView viewWithTag:tag] is *really* slow with 100s of sub-views. I created an NSDictionary of my 100s of subviews and kablam! That issue was gone!
- I had used an NSDictionary in LifeBoard to store the state in. I would create a key for a cell by [NSString stringWithFormat:@"%i.%i", x, y]. Another *super* slow idea. Changed to [NSNumber numberWithInteger:(y * board.width + x)]. That was faster.
- I then moved from an NSDictionary for storing the board to the following:
- char* theBoard;
- theBoard = (char*)malloc(width*height);
- -(void)dealloc { free(theBoard); }
- Now that is FAST.
The new-and-improved version runs ata reasonable frame rate, even at large board sizes (150x150 runs several frames a second).
Please see the upgraded code on GitHub.