【发布时间】:2019-02-21 07:45:10
【问题描述】:
下面的代码是使用一个完整的核心性能,速度很慢,需要10-12秒才能完成,我怎样才能让它更快?
NetworkStream networkStream = (NetworkStream)param;
byte[] sendBytes = new byte[1000000];
int current = 0;
int cpos = 0;
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
sendBytes[current] = i;
current++;
sendBytes[current] = j;
current++;
sendBytes[current] = k;
current++;
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
for (int z = 0; z < 16; z++)
{
sendBytes[current] = getItem(x + i * 16, y + j * 16, z + k * 16);
current++;
}
}
}
//Here it also copies the old bytes and compresses them using zlibstream
networkStream.Write(sendBytes, 0, current);
current = 0;
}
}
}
byte getItem(int x, int y, int z)
{
return blockIds[x, y, z];
}
编辑:添加了缺少的方法代码。
【问题讨论】:
-
什么是循环中的循环中的循环中的循环中的循环?
-
请问最初的问题是什么(你试图用深层嵌套循环来解决)?
-
有方块但不是我的世界的游戏
-
嗯,你打电话给
getItem1600 万次。我们不知道它在做什么,这很难提供帮助,但如果这不是“非常非常快”,那么它很慢也就不足为奇了。 -
你为什么不直接写
blockIds?
标签: c# performance for-loop