Question

My game of life is coded for a 3D enviroment, written in C# using SharpGL. The code works fine, as far as the logic goes but it runs really slowly.

The cells in the game are color coded, which means that depending on their state for the next "turn," they will change color. My teacher is going to test the project on some really old computers, so I need to figure out how to make the code faster and more efficient.

The calculation code is posted below. Please point out any problems that might slow down the game.

//cell.willdie means that the cell will die in the next turn
//cell.dies means that the cell dies immediately
//cell.abouttobeborn means that the cell will be born in the next turn
//cell.coord is where the cell is

public void checkcells() //determines whether or not for every cell if they die or not. Also changes color-coding of cells.
        {
            List<int[]> openspaceatwhere = new List<int[]>();
            List<cell_class> acell = new List<cell_class>();
            for (int i = 0; i < cell.Count(); i++)
            {
                bool dies = false;
                int neighbors = 0;
                cell[i].willdie = false;
                for (int x = -1; x < 2; x++)
                {
                    for (int y = -1; y < 2; y++)
                    {
                        for (int z = -1; z < 2; z++)
                        {
                            bool isopen = true;
                            for (int i2 = 0; i2 < cell.Count(); i2++)
                            {
                                if (!(x == 0 && y == 0 && z == 0) && cell[i].coord[0] + x == cell[i2].coord[0] && cell[i].coord[1] + y == cell[i2].coord[1] && cell[i].coord[2] + z == cell[i2].coord[2])
                                {
                                    isopen = false;
                                    neighbors += 1;
                                }
                            }
                            if (isopen)
                            {
                                openspaceatwhere.Add(new int[4] { cell[i].coord[0]+x, cell[i].coord[1]+y, cell[i].coord[2]+z, 0 });
                            }
                        }
                    }
                }
                if (neighbors > 6 || neighbors < 3)
                    dies = true;
                if (dies)
                    cell[i].dies = true;
                else
                    cell[i].dies = false;
                for (int a = 0; a < openspaceatwhere.Count(); a++)
                {
                    for (int b = 0; b < openspaceatwhere.Count(); b++)
                    {
                        if (openspaceatwhere[a][0] == openspaceatwhere[b][0] && openspaceatwhere[a][1] == openspaceatwhere[b][1] && openspaceatwhere[a][2] == openspaceatwhere[b][2])
                        {
                            if (a != b)
                            { openspaceatwhere.RemoveAt(b); openspaceatwhere[a][3] += 1; }
                        }
                    }
                }
            }
            List<int[]> quequecell = new List<int[]>();
            for (int i = 0; i < openspaceatwhere.Count(); i++ )
            {
                if(openspaceatwhere[i][3] == 6)
                    quequecell.Add(new int[3] { openspaceatwhere[i][0], openspaceatwhere[i][1], openspaceatwhere[i][2]});
            }
            for (int i = 0; i < cell.Count(); i++)
            {
                if (cell[i].dies)
                {
                    if (animated && delay == 50)
                    {
                        cell.RemoveAt(i);
                        cellsdied += 1;
                        i--;
                    }
                    else
                    {
                        cell[i].willdie = true;
                    }
                }
            }
            for (int i = 0; i < quequecell.Count(); i++)
            {
                if (animated && delay == 50)
                {
                    cell.Add(new cell_class());
                    cell[cell.Count()-1].coord[0] = quequecell[i][0];
                    cell[cell.Count()-1].coord[1] = quequecell[i][1];
                    cell[cell.Count()-1].coord[2] = quequecell[i][2];
                    createdcells += 1;
                }
                else
                {
                    acell.Add(new cell_class());
                    acell[acell.Count()-1].coord[0] = quequecell[i][0];
                    acell[acell.Count()-1].coord[1] = quequecell[i][1];
                    acell[acell.Count()-1].coord[2] = quequecell[i][2];
                    acell[acell.Count()-1].abouttobeborn = true;
                    rendercell(acell[acell.Count()-1]);
                }
            }
        }
Was it helpful?

Solution

  1. Make sure that this piece of code is actually the performance bottle neck. Even though you are using xxxGL, drawing may be time consuming.
  2. Review the list types you are using and make sure they are efficient for the operations you are performing on them. For example, depending on the type of the cell list,

        for (int i = 0; i < cell.Count(); i++)
        {
            if (cell[i].dies)
            {
                if (animated && delay == 50)
                {
                    cell.RemoveAt(i);
                    cellsdied += 1;
                    i--;
                }
                else
                {
                    cell[i].willdie = true;
                }
            }
        }
    

this code may be really slow because of the RemoveAt(i), i-- style you are using to iterate.

3.Consider keeping some of your data structures around and updating them instead of building new lists and objects. A cell can be thought of to exist for the lifetime of the game. Sometimes it is dead, other times it is dying, or alive, but the cell object does not need to be deleted or recreated. I'm a little outside of my knowledge base with C#, but calls to 'new' involve memory allocation, and are more expensive than keeping an object around and modifying its state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top