EK2*_*206 2 .net c# performance visual-studio-2013
我有一个方法,我做以下事情
然而,尽管参数没有变化(只有值改变),方法的执行时间也在不断增加.
以下是我的方法的内容:
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();
Run Code Online (Sandbox Code Playgroud)
经历的时间如下:
95,95,104,109,192,201,218,418,447,452,459,495,504,528,1060,1065,1072,1146,1154等
你Stopwatch
从它被停止的那一刻开始.你需要Stopwatch.Restart
方法,或者你可以Stopwatch.Reset
在开始之前调用方法.
sw.Reset();
sw.Start();
Run Code Online (Sandbox Code Playgroud)
要么
sw.Restart();
Run Code Online (Sandbox Code Playgroud)