简体   繁体   中英

How can I make this big C# loop faster?

The code under is using one full core performace and it's very slow that it takes 10-12 seconds to finish, how can I make it faster?

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];
}

Edit: Added the missing method code.

Note that sending saved thing takes only 0.1 seconds

I guess it's the duration for 1 call to NetworkStream.Write method

A simple test by sending data to loopback. It costs about 300ms on my PC, and 50ms without sending data. So obviously the reason is the speed of your network.

The loop is fast enough

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM