我使用太多for循环,如果我的代码条件但我的性能变低......如何提高我的代码的性能..这里是代码 [英] I am using too many for loops and if conditions in my code but my performance is getting low... how can increase performance for my code.. here is code

查看:59
本文介绍了我使用太多for循环,如果我的代码条件但我的性能变低......如何提高我的代码的性能..这里是代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if (ProviderCountHealthConditions >   0 
{
for int i = 0 ; i < ProviderCountHealthConditions1; i ++)
{

if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString()== HealthConditionsQ12
{
// 1st内行
if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString()== ParalysisOrAmputation
{

iTextSharp.text.pdf.PdfPTable tblHealthConditionsinner = new iTextSharp.text.pdf.PdfPTable( 2 );
tblHealthConditionsinner.TotalWidth = 550f;
tblHealthConditionsinner.AddCell(GetCellImage(chkdImg));
tblHealthConditionsinner.AddCell(GetInnerCell( ParalysisOrAmputation 2 1 ));

tblHealthConditions.AddCell(tblHealthConditionsinner);


}
for (i = 0 ; i < ProviderCountHealthConditions1; i ++)
{
if (iPatientInfoHealthConditions.ElementAtOrDefault (i).QCode.ToString()== HealthConditionsQ24
{
iTextSharp.text.pdf.PdfPTable tblinner = new iTextSharp.text.pdf.PdfPTable( 4 ) ;
tblinner.TotalWidth = 10f;
tblinner.AddCell(GetCellImage(chkdImg));
tblinner.AddCell(GetInnerCell( 瘫痪 1 1 ));
for (i = 0 ; i < ; ProviderCountHealthConditions1; i ++)
{
if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString()== HealthConditionsQ18
{
if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString()== ParalysisOrAmputation
{
tblinner.AddCell( new iTextSharp.text.Phrase( DESCRIBE: + iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorText.ToString(),fText));



}

}
如果(iPatientInfoHealthConditions.ElementAtOrDefault( i).QCode.ToString()== HealthConditionsQ19
{
if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString()== ParalysisOrAmputation
{

tblinner.AddCell( new iTextSharp.text.Phrase ( 移动模式: + iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorT ext.ToString(),fText));


}

}
}
tblHealthConditions.AddCell(tblinner);

}
}

}

解决方案

你是循环遍历iPatientInfoHealthConditions中的所有项目,只是为了找到你想要的1项。然后你经常在寻找它之后扔掉它。



我不知道列表有多大,但这不是必需的。你可以使用linq得到你需要的物品:



使用IEnumerable.Any([condition])检查物品是否存在。

使用IEnumerable.SingleOrDefault([condition])查找特定项目,然后检查它是否为null(null表示未找到)



  if (ProviderCountHealthConditions >   0 
{
if (iPatientInfoHealthConditions.Any(pihc = > pihc.QCode.ToString()== HealthConditionsQ12&& pihc.FactorDesc.ToString ()== ParalysisOrAmputation))
{
iTextSharp.text。 pdf.PdfPTable tblHealthConditionsinner = new iTextSharp.text.pdf.PdfPTable( 2 );
tblHealthConditionsinner.TotalWidth = 550f;
tblHealthConditionsinner.AddCell(GetCellImage(chkdImg));
tblHealthConditionsinner.AddCell(GetInnerCell( ParalysisOrAmputation 2 1 ));

tblHealthConditions.AddCell(tblHealthConditionsinner);
}

if (iPatientInfoHealthConditions.Any(pihc = > pihc.QCode.ToString()== HealthConditionsQ24))
{
iTextSharp.text.pdf.PdfPTable tblinner = new iTextSharp.text.pdf.PdfPTable( 4 ) ;
tblinner.TotalWidth = 10f;
tblinner.AddCell(GetCellImage(chkdImg));
tblinner.AddCell(GetInnerCell( 瘫痪 1 1 ));

var healthConditionsQ18 =
iPatientInfoHealthConditions.SingleOrDefault(pihc = > pihc.QCode.ToString()== HealthConditionsQ18&& pihc.FactorDesc.ToString ()== ParalysisOrAmputation);
if (healthConditionsQ18!= null
{
tblinner。 AddCell( new iTextSharp.text.Phrase( DESCRIBE: + healthConditionsQ18.FactorText.ToString(),fText));

}
var healthConditionsQ19 =
iPatientInfoHealthConditions.SingleOrDefault(pihc = > pihc.QCode.ToString()== HealthConditionsQ19&& pihc.FactorDesc.ToString()== ParalysisOrAmputation);

if (healthConditionsQ19!= null
{
tblinner.AddCell(
new iTextSharp.text.Phrase( 移动模式: + healthConditionsQ19.FactorText.ToString(),fText));

}

tblHealthConditions.AddCell(tblinner);
}
}


1)跳过不必要的迭代。

正如Naveen.Sanagasetti在评论中所说,尽可能跳过列表的大部分如果你事先能够告诉你这部分不适合 if 条件。



2)不要比较ToString()。

如果条件比较 ToString()的结果用字符串文字。如果可能,直接比较 QCode FactorDesc ,而不调用ToString()。由于没有关于其类型的信息,我不能详细说明。但如果QCode是一个整数(无论大小)或枚举,比较将比第一次转换快得多。



3)暂停UI更新

由于您没有指定UI技术,我将假设Windows窗体。有可能 AddCell()每次调用时都会刷新其表。如果经常这样,那将会非常耗时。

你可以暂停UI布局,执行所有循环,检查和添加,然后恢复UI布局,一次性对屏幕进行所有UI更改。

这样做的速度更快,因为每一个小的变化都必须在整个UI周围(或至少在其右边或底部)进行修改,而一个大的变化只需要做一次。


< blockquote>以下是代码示例中的一些一般性想法(将编译,但 不是工作解决方案)。



我希望你从这个例子中得到的想法是:



1.在开始循环之前消除可能的错误



2.使用switch / case语句进行改进d速度,以及更长的代码可维护性。如果使用整数开关标准,特别是如果整数是线性序列,即0,1,2 ...等......这将被编译为跳转表,并将提供最大性能。 br />


假设HealthCondition(猜测!)类似于:

  public   class  ProviderHealthCondition 
{
public int 索引{ set ; get ; }
public string ConditionName { set ; get ; }
public List< string>因素{设置; get ; }

public ProviderHealthCondition( int index,字符串条件,参数 字符串 []因子)
{
指数=指数;
ConditionName = condition;
因子= factors.ToList< string>();
}
}

public static class TreatmentMap
{
public static void MapConditionsToTreatment(List< ProviderHealthCondition>条件)
{
if (conditions.Count == 0 throw new ArgumentException( error:0 ProviderCountHealthConditions);

ProviderHealthCondition currentCondition;
string currentQCode;
string currentFactor;

for int i = 0 ; i < conditions.Count; i ++)
{
currentCondition = conditions [i];

currentQCode = currentCondition.ConditionName;

// 不工作......因为我看不出你如何选择哪个因素
// 在您的代码中使用
currentFactor = currentCondition .Factors [I];

switch (currentQCode)
{
case HealthConditionsQ12
// 打开当前因子
开关(currentFactor)
{
case ParalysisOrAmputation

break ;

case CantWalkAndChewGum

break ;
}
break ;

case HealthConditionsQ24

break ;

// 依此类推...

默认
break ;
}
}
}
}






if (ProviderCountHealthConditions > 0)
                {
                    for (int i = 0; i < ProviderCountHealthConditions1; i++)
                    {

                        if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ12")
                        {
                            //1st inner row
                            if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString() == "ParalysisOrAmputation")
                            {

                                iTextSharp.text.pdf.PdfPTable tblHealthConditionsinner = new iTextSharp.text.pdf.PdfPTable(2);
                                tblHealthConditionsinner.TotalWidth = 550f;
                                tblHealthConditionsinner.AddCell(GetCellImage(chkdImg));
                                tblHealthConditionsinner.AddCell(GetInnerCell("ParalysisOrAmputation", "", 2, 1));

                                tblHealthConditions.AddCell(tblHealthConditionsinner);


                            }
                            for (i = 0; i < ProviderCountHealthConditions1; i++)
                            {
                                if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ24")
                                {
                                    iTextSharp.text.pdf.PdfPTable tblinner = new iTextSharp.text.pdf.PdfPTable(4);
                                    tblinner.TotalWidth = 10f;
                                    tblinner.AddCell(GetCellImage(chkdImg));
                                    tblinner.AddCell(GetInnerCell("Paralysis", "", 1, 1));
                                    for (i = 0; i < ProviderCountHealthConditions1; i++)
                                    {
                                        if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ18")
                                        {
                                            if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString() == "ParalysisOrAmputation")
                                            {
                                                tblinner.AddCell(new iTextSharp.text.Phrase("DESCRIBE: " + iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorText.ToString(), fText));



                                            }

                                        }
                                        if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ19")
                                        {
                                            if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString() == "ParalysisOrAmputation")
                                            {

                                                tblinner.AddCell(new iTextSharp.text.Phrase("Mode of Mobility:" + iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorText.ToString(), fText));


                                            }

                                        }
                                    }
                                    tblHealthConditions.AddCell(tblinner);

                                }
                            }

                        }

解决方案

You're looping through all items in iPatientInfoHealthConditions just to find the 1 item you're after. You then often throw that item away after looking for it.

I have no idea how large the list, but that's not required. You can just get the item you need using linq:

Use IEnumerable.Any([condition]) to check if an item exists.
Use IEnumerable.SingleOrDefault([condition]) to find a specific item, then check that it is not null (null means it was not found)

if (ProviderCountHealthConditions > 0)
{
    if (iPatientInfoHealthConditions.Any(pihc => pihc.QCode.ToString() == "HealthConditionsQ12" && pihc.FactorDesc.ToString() == "ParalysisOrAmputation"))
    {
        iTextSharp.text.pdf.PdfPTable tblHealthConditionsinner = new iTextSharp.text.pdf.PdfPTable(2);
        tblHealthConditionsinner.TotalWidth = 550f;
        tblHealthConditionsinner.AddCell(GetCellImage(chkdImg));
        tblHealthConditionsinner.AddCell(GetInnerCell("ParalysisOrAmputation", "", 2, 1));

        tblHealthConditions.AddCell(tblHealthConditionsinner);
    }

    if (iPatientInfoHealthConditions.Any(pihc => pihc.QCode.ToString() == "HealthConditionsQ24"))
    {
        iTextSharp.text.pdf.PdfPTable tblinner = new iTextSharp.text.pdf.PdfPTable(4);
        tblinner.TotalWidth = 10f;
        tblinner.AddCell(GetCellImage(chkdImg));
        tblinner.AddCell(GetInnerCell("Paralysis", "", 1, 1));
                    
            var healthConditionsQ18 =
                iPatientInfoHealthConditions.SingleOrDefault(pihc => pihc.QCode.ToString() == "HealthConditionsQ18" && pihc.FactorDesc.ToString() == "ParalysisOrAmputation");
            if (healthConditionsQ18 != null)
            {
                tblinner.AddCell(new iTextSharp.text.Phrase("DESCRIBE: " + healthConditionsQ18.FactorText.ToString(), fText));

            }
            var healthConditionsQ19 =
                iPatientInfoHealthConditions.SingleOrDefault(pihc => pihc.QCode.ToString() == "HealthConditionsQ19" && pihc.FactorDesc.ToString() == "ParalysisOrAmputation");

            if (healthConditionsQ19!=null)
            {
                    tblinner.AddCell(
                        new iTextSharp.text.Phrase("Mode of Mobility:" + healthConditionsQ19.FactorText.ToString(), fText));
                            
            }
                    
        tblHealthConditions.AddCell(tblinner);
    }
}


1) Skip unnecessary iterations.
As Naveen.Sanagasetti said in a comment, skip as large portions of lists as possible if you are able to tell beforehand that this portion won't fit your if condition.

2) Don't ToString() to compare.
Your if conditions compare the result of ToString() with string literals. If possible, compare QCode and FactorDesc directly, without calling ToString(). Since there's no information about their types, I can't go into details on that. But if QCode is an integer (of whatever size) or enumeration, a comparison will be significantly faster than first converting it.

3) Suspend UI updates
Since you didn't specify a UI technology, I'll assume Windows Forms. Chances are that AddCell() will refresh its table every time it is called. If that's often, it will get surprisingly time-consuming.
You can suspend UI layout, perform all your loops, checks and additions and then resume UI layout to get all UI changes to the screen in one go.
That's faster because each one of the small changes has to rework the complete UI around (or at least to the right or bottom of) it, whilst one big change has to do that only once.


Here are some general ideas in a code example (that will compile, but is not a working solution).

The ideas I hope you will get from this example are:

1. eliminate possible errors before you start loops

2. use switch/case statements for improved speed, and for greater code maintainability over time. If you use an integer switch criterion, particularly if the integers are in a linear sequence, i.e., 0,1,2 ... etc. ... this will be compiled as a jump-table, and will give maximum performance.

Assuming a "HealthCondition" (a guess !) is something like:

public class ProviderHealthCondition
{
    public int Index { set; get; }
    public string ConditionName { set; get; }
    public List<string> Factors { set; get; }

    public ProviderHealthCondition(int index, string condition, params string[] factors)
    {
        Index = index;
        ConditionName = condition;
        Factors = factors.ToList<string>();
    }
}

public static class TreatmentMap
{
    public static void MapConditionsToTreatment(List<ProviderHealthCondition> conditions)
    {
        if (conditions.Count == 0) throw new ArgumentException("error: 0 ProviderCountHealthConditions");

        ProviderHealthCondition currentCondition;
        string currentQCode;
        string currentFactor;
        
        for (int i = 0; i < conditions.Count; i++)
        {
            currentCondition = conditions[i];

            currentQCode = currentCondition.ConditionName;

            // not working ... since I can't see how you select which Factor
            // to use in your code
            currentFactor = currentCondition.Factors[i];

            switch (currentQCode)
            {
                case "HealthConditionsQ12":
                    // switch on the current Factor
                    switch (currentFactor)
                    {
                        case "ParalysisOrAmputation":

                            break;

                        case "CantWalkAndChewGum":

                            break;
                    }
                   break;

                case "HealthConditionsQ24":

                    break;

                // and so on ...

                default:
                    break;
            }
        }
    }
}



[Edit: MTH: Just a couple of markup errors. No logic changes.]


这篇关于我使用太多for循环,如果我的代码条件但我的性能变低......如何提高我的代码的性能..这里是代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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