在此代码段中如何提高性能并减少内存分配? [英] what to do to improve performance and reduce memory allocation in this code snippet?

查看:56
本文介绍了在此代码段中如何提高性能并减少内存分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,以提高性能并在此代码段中分配内存分配

kindly help me out so as to improve performance and dispose memory allocation in this code snippet

public static void GetBuildingCount(int marineCount, int[,] marinePositions)
       {
               int x = 0;
               int i = 0;
               int j = 0;
               bool processed = false;
               if (marineCount == (marinePositions.Length / 2))
               {
                   for (j = 1; j <= 8; j++)
                   {
                       processed = false;
                       for (i = 0; i <= marinePositions.Length / 2 - 1; i++)
                       {
                           if (j == marinePositions[i, 0] && processed == false)
                           {
                               x++;
                               output1 = output1 + 8;
                               processed = true;
                           }
                       }

                   }
                   for (j = 1; j <= 8; j++)
                   {
                       processed = false;
                       for (i = 0; i <= marinePositions.Length / 2 - 1; i++)
                       {
                           if (j == marinePositions[i, 1] && processed == false)
                           {
                               output1 = output1 + 8 - x;
                               processed = true;
                           }
                       }
                   }
               }

               System.GC.Collect();
               System.GC.WaitForPendingFinalizers();

           }

推荐答案

如果要提高性能,首先要注意的是停止执行相同的循环两次.您不能将两者结合在一起吗?

另外,请勿强行进行垃圾收集.在用.NET编写应用程序的所有时间里,我都不需要强制垃圾收集器进行处理(这样做会导致性能下降).
If you want to improve performance, the first thing you should look at is to stop performing the same loop twice. Is there a reason you cannot combine the two together?

Also, don''t force a garbage collection. In all the time I''ve been coding apps in .NET, I have never needed to force the garbage collector to process (and doing so causes a performance hit).


首先,沟您的processed变量,并改用break.这将导致该循环立即终止,而不是在找到所需的内容之后继续运行.
Firstly, ditch your processed variable, and use break instead. This will cause that loop to terminate immediately, instead of running through after you have found what you want.
for (j = 1; j <= 8; j++)
{
    for (i = 0; i <= marinePositions.Length / 2 - 1; i++)
    {
        if (j == marinePositions[i, 0])
        {
            x++;
            output1 = output1 + 8;
            break;
        }
    }
}

这适用于两个"j"循环.
其次,转储垃圾回收的东西-此例程不会创建或处置GC可以处理的任何数据-都是基于本地堆栈的变量.

This applies to both "j" loops.
Secondly, dump the Garbage Collection stuff - this routine does not create or dispose any data that the GC can handle - it is all local stack based variables.


如果您正在寻找Task经理,看看您的应用程序正在使用多少内存,将其停止. TM不会向您显示您的应用程序正在使用多少内存,而是向您显示运行您的应用程序的.NET CLR保留了多少内存.您不必担心. CLR在管理内存以及在必要时将所有未使用的内存返回给Windows方面做得很好.
If you''re looking in Task Manager to see how muchmemory your app is using, stop it. TM isn''t showing you how much memory your app is using, but is showing you how much memory is RESERVED by the .NET CLR running your app. You''ve got nothing to worry about. The CLR does a great job at managing memory and returning any unused memory back to Windows when necessary.


这篇关于在此代码段中如何提高性能并减少内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆