C# 为什么这个功能这么慢?

C# 为什么这个功能这么慢?,c#,xna,C#,Xna,我似乎不明白为什么它这么慢,所以我希望其他人知道:/。块顶点和块索引是列表 public void get_cube_at_position(int x, int y, int z,Color colour) { int length; if (y > y_size - 2) { chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y +

我似乎不明白为什么它这么慢,所以我希望其他人知道:/。块顶点和块索引是列表

    public void get_cube_at_position(int x, int y, int z,Color colour)
    {
        int length;
        if (y > y_size - 2)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);

        }
        else if (blocks[x, y + 1, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);

        }
        if ( y != 0 && blocks[x, y - 1, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
        if (x > x_size - 2)
        {
        }
        else if (blocks[x + 1, y, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, 1 + y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, 1 + y, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
        if (x != 0 && blocks[x - 1, y, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
        if (z > z_size - 2)
        {
        }
        else if (blocks[x, y, z + 1] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);

        }
        if (z != 0 && blocks[x, y, z - 1] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
    }
您在该方法中所做的任何事情本质上都不是一个缓慢的操作

你可以做一些微优化来节省一些时间。但是没有什么实质性的东西

在此方法之外对列表所做的操作可能会导致
Add
方法的速度大大慢于此(例如定期使用
new
重新创建它们)

但真正的问题可能是,你只是每秒调用150000次(正如你在评论中提到的)。你应该经常提到这样的东西——因为在一种情况下速度快的东西在另一种情况下可能慢

此外,假设您正在将此方法生成的数据加载到GPU上(可能是通过将其复制到新阵列),每帧至少加载一次。这可能太频繁了,也是速度损失的真正原因

您可能应该做的是在加载时生成所有顶点/索引数据,然后仅在绘制时引用它


或者,如果您需要在游戏运行时修改数据,请找出方法来减少调用此方法的频率,并在执行此操作时减少对数据的接触(例如:仅在必要时重新生成,仅重新生成修改后的数据等)。

定义“缓慢”。慢与什么相比?OP如何定义慢有关系吗?只需查看代码并尝试找到可能的优化方法。@Marlon:我不确定“优化我的代码”是一个很好的问题……是的,编写这段代码一定花了一段时间。使用剖析器。@OliCharlesworth显然,这些类型的问题不属于SO。但是要求OP定义代码的速度,这显然是一个游戏,需要一些性能。。。这只是一个毫无意义的问题。该方法每秒最多调用150000次。在这一点上,代码有多“慢”并不重要,但它能有多“快”。