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

标签 c# .net performance visual-studio-2013

我有一个方法可以执行以下操作

  1. 我向类对象添加值
  2. 使用 Entity FrameWork 检查数据库中的差异

然而,尽管参数没有变化(只有值发生变化),但该方法的执行时间不断增加。

下面是我的方法的内容:

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.Restart方法,或者您可以在开始之前调用 Stopwatch.Reset 方法。

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

sw.Restart();

关于c# - 方法的执行时间在增加,为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25406617/

相关文章:

C# 数据绑定(bind) : Create object if null

从套接字接收数据的 C# 性能方法?

javascript - Chrome 堆快照在每次刷新时增长.. 正常吗?

python - 如何提高这个计数程序的性能?

c# - 如何从 View 中的 FOREACH 语句输出 MVC 中的字符串列表?

C# Mono+Winforms MessageBox问题

c# - WPF 项目中的语义版本控制

c# - Flul 和不受信任的证书

.net - 在 Windows Powershell(或 dotNet 又名 .net)中执行 textwrap 和 dedent

c# - 如何在没有任何 nuspec 文件的情况下将多个项目的产品打包到一个 nuget 中?