【问题标题】:How can I make this big C# loop faster?我怎样才能使这个大的 C# 循环更快?
【发布时间】: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];
}

编辑:添加了缺少的方法代码。

【问题讨论】:

  • 什么是循环中的循环中的循环中的循环中的循环?
  • 请问最初的问题是什么(你试图用深层嵌套循环来解决)?
  • 有方块但不是我的世界的游戏
  • 嗯,你打电话给getItem 1600 万次。我们不知道它在做什么,这很难提供帮助,但如果这不是“非常非常快”,那么它很慢也就不足为奇了。
  • 你为什么不直接写blockIds

标签: c# performance for-loop


【解决方案1】:

注意发送保存的东西只需要 0.1 秒

我猜这是 1 次调用 NetworkStream.Write 方法的持续时间

通过将数据发送到环回的简单测试。在我的 PC 上花费大约 300 毫秒,不发送数据需要 50 毫秒。很明显,原因是您的网络速度。

循环足够快

//Local server
Socket server = new Socket(SocketType.Stream, ProtocolType.Tcp);
Socket peer = null;
server.Bind(new IPEndPoint(IPAddress.Any, 29999));
server.Listen(50);
server.BeginAccept(ar =>
{
    peer = server.EndAccept(ar);
    byte[] buf = new byte[16 * 16 * 16 + 3];
    while (peer.Connected)
        peer.Receive(buf);
},
null);

//Client
Socket client = new Socket(SocketType.Stream, ProtocolType.Tcp);
client.Connect(new IPEndPoint(IPAddress.Loopback, 29999));
var networkStream = new NetworkStream(client);

//Loop
byte[] sendBytes = new byte[16 * 16 * 16 + 3];
int current = 0;
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 16; i++)
    for (int j = 0; j < 16; j++)
        for (int k = 0; k < 16; k++)
        {
            sendBytes[current++] = (byte)i;
            sendBytes[current++] = (byte)j;
            sendBytes[current++] = (byte)k;

            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);

            networkStream.Write(sendBytes, 0, current);
            current = 0;
        }
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);

//Clean works
client.Close();
peer.Close();
server.Close();

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2020-02-20
    • 2023-03-18
    • 1970-01-01
    • 2021-11-04
    • 2020-08-14
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多