Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法的执行时间在增加,为什么会这样?_C#_.net_Performance_Visual Studio 2013 - Fatal编程技术网

C# 方法的执行时间在增加,为什么会这样?

C# 方法的执行时间在增加,为什么会这样?,c#,.net,performance,visual-studio-2013,C#,.net,Performance,Visual Studio 2013,我有一个方法,在这里我做以下事情 我向类对象添加值 使用实体框架检查数据库中的差异 然而,尽管参数没有变化(只有值发生变化),该方法的执行时间仍在不断增加 以下是我的方法的内容: sw.Start(); details.EffectiveDate = cb.Attribute("dte_effective").Value.GetDate(); details.EndDate = cb.Attribute("dte_end").Value.GetDate(); details.MaxRefill

我有一个方法,在这里我做以下事情

  • 我向类对象添加值
  • 使用实体框架检查数据库中的差异
  • 然而,尽管参数没有变化(只有值发生变化),该方法的执行时间仍在不断增加

    以下是我的方法的内容:

    sw.Start();
    
    details.EffectiveDate = cb.Attribute("dte_effective").Value.GetDate();
    details.EndDate = cb.Attribute("dte_end").Value.GetDate();
    details.MaxRefills = cb.Attribute("qty_refill").Value.ToIntOrNull();
    details.Sex = cb.Attribute("cde_sex").Value == "B" || string.IsNullOrWhiteSpace(cb.Attribute("cde_sex").Value) ? (string)null : cb.Attribute("cde_sex").Value.Substring(0, 1);
    details.RxLimit = cb.Attribute("cde_rx_limit").Value.ToBoolOrNull();
    details.WEAC = fullDoc.Descendants("weacPrice").Select(d => (decimal?)decimal.Parse(d.Attribute("amt_npt_price").Value)).FirstOrDefault();
    details.EAC = fullDoc.Descendants("eacPrice").Select(d => (decimal?)decimal.Parse(d.Attribute("amt_npt_price").Value)).FirstOrDefault();
    details.FederalMAC = fullDoc.Descendants("fulPrice").Select(d => (decimal?)decimal.Parse(d.Attribute("amt_mac").Value)).FirstOrDefault();
    details.StateMAC = fullDoc.Descendants("macPrice").Select(d => (decimal?)decimal.Parse(d.Attribute("amt_mac").Value)).FirstOrDefault();
    //there are few more
    
    var currRestrictions = db.NDCDiagRestrictions.Where(n => n.NDC == NDC).ToList();
    
    var newRestrictions = fullDoc.Descendants("diagRestriction")
                                 .Select(d => new NDCDiagRestriction()
                                 {
                                     NDC = NDC,
                                     DiagFrom = long.Parse(d.Attribute("cde_diag_from").Value),
                                     DiagTo = long.Parse(d.Attribute("cde_diag_to").Value),
                                     EffectiveDate = d.Attribute("dte_effective").Value.GetDate(),
                                     EndDate = d.Attribute("dte_end").Value.GetDate()
                                 })
                                 .ToList();
    
    var joined = from n in newRestrictions
                 from c in currRestrictions
                 where n.DiagTo == c.DiagTo
                    && n.EffectiveDate == c.EffectiveDate
                    && n.EndDate == c.EndDate
                    && n.DiagFrom == c.DiagFrom
                    && n.NDC == c.NDC
                 select n.NDC;
    
    if (newRestrictions.Count != currRestrictions.Count
        || newRestrictions.Count != joined.Count())
    {
        foreach (var rm in currRestrictions)
            db.NDCDiagRestrictions.Remove(rm);
    
        foreach (var ad in newRestrictions)
            db.NDCDiagRestrictions.Add(ad);
    }
    
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds.ToString());
    
    sw.Restart();
    
    经过的时间如下:


    95、95、104、109、192、201、218、418、447、452、459、495、504、528、1060、1065、1072、1146、1154等。您正在从停止点再次启动秒表。您需要方法,或者您可以在开始之前调用
    Stopwatch.Reset
    method

    sw.Reset();
    sw.Start();
    


    你确定你真的在重置秒表吗?显示所有相关的代码很重要,您还没有包括
    sw
    的声明。如果所有与性能相关的问题都这么简单就好了;)我有秒表。重新启动()我忘了包括…..对不起,我道歉。。这是之后的输出…为什么要在方法结束时重新启动秒表?通过这样做,您可以对从方法结束到下一次开始的任何事情进行计时。
    sw.Restart();