0

私のライフ ゲームは、SharpGL を使用して C# で記述された 3D 環境用にコーディングされています。ロジックに関する限り、コードは正常に動作しますが、実行速度は非常に遅くなります。

ゲーム内のセルは色分けされています。つまり、次の「ターン」の状態に応じて色が変わります。私の先生は、かなり古いコンピューターでプロジェクトをテストする予定なので、コードをより高速かつ効率的にする方法を見つける必要があります。

計算コードを以下に掲載します。ゲームを遅くする可能性のある問題を指摘してください。

//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]);
                }
            }
        }
4

1 に答える 1

2
  1. このコードが実際にパフォーマンスのボトルネックであることを確認してください。xxxGL を使用していても、描画に時間がかかる場合があります。
  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;
                }
            }
        }
    

このコードは、繰り返しに使用している RemoveAt(i), i-- スタイルのため、非常に遅くなる可能性があります。

3.新しいリストやオブジェクトを作成する代わりに、データ構造の一部を保持して更新することを検討してください。セルは、ゲームの存続期間中存在すると考えることができます。死んでいる場合もあれば、死んでいる、または生きている場合もありますが、セル オブジェクトを削除したり再作成したりする必要はありません。私は C# の知識ベースから少し外れていますが、'new' の呼び出しにはメモリ割り当てが必要であり、オブジェクトを保持してその状態を変更するよりもコストがかかります。

于 2013-06-03T17:54:50.347 に答える