![](/img/trans.png)
[英]NullRefernceException when adding XElement to a XDocument
[英]Adding 90000 XElement to XDocument
我有一个Dictionary<int, MyClass>
它包含100,000个项目
填充了10,000个项目的值,而90,000个为空。
我有以下代码:
var nullitems = MyInfoCollection.Where(x => x.Value == null).ToList();
nullitems.ForEach(x => LogMissedSequenceError(x.Key + 1));
private void LogMissedSequenceError(long SequenceNumber)
{
DateTime recordTime = DateTime.Now;
var errors = MyXDocument.Descendants("ERRORS").FirstOrDefault();
if (errors != null)
{
errors.Add(
new XElement("ERROR",
new XElement("DATETIME", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")),
new XElement("DETAIL", "No information was read for expected sequence number " + SequenceNumber),
new XAttribute("TYPE", "MISSED"),
new XElement("PAGEID", SequenceNumber)
)
);
}
}
这似乎需要大约2分钟才能完成。 我似乎找不到瓶颈所在,或者这个时机听起来正确吗?
谁能看到为什么花这么长时间的东西?
如果您的MyInfoCollection
很大,我不会在上面调用ToList()
以便可以使用ForEach
扩展方法。 调用ToList()
将创建并填充一个巨大的列表。 我将删除ToList()
调用,并将.ForEach
放入for each
语句的,或编写一个。 IEnumerable<T>
ForEach
扩展方法。
然后分析它,并查看需要多长时间。 要做的另一件事是删除ERRORS
元素的find和null检查。 如果不存在,则不要for each
上述for each
语句调用。 这样一来,您可以将其空检查一次而不是90,000次。
另外,正如Michael Stum指出的那样,我将定义一个字符串以保存值DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")
,然后引用它或将其传递。甚至不要使用此调用:
DateTime recordTime = DateTime.Now;
这是我最有可能做的。
private void BuildErrorNodes()
{
const string nodeFormat = @"<ERROR TYPE=""MISSED""><DATETIME>{0}</DATETIME><DETAIL>No information was read for expected sequence number {1}</DETAIL><PAGEID>{1}</PAGEID></ERROR>";
var sb = new StringBuilder("<ERRORS>");
foreach (var item in MyInfoCollection)
{
if (item.Value == null)
{
sb.AppendFormat(
nodeFormat,
DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff"),
item.Key + 1
);
}
}
sb.Append("</ERRORS>");
var errorsNode = MyXDocument.Descendants("ERRORS").FirstOrDefault();
errorsNode.ReplaceWith(XElement.Parse(sb.ToString()));
}
如何用LINQ查询替换方法调用?
static void Main(string[] args)
{
var MyInfoCollection = (from key in Enumerable.Range(0, 100000)
let value = (MoreRandom() % 10 != 0)
? (string)null
: "H"
select new { Value = value, Key = key }
).ToDictionary(k => k.Key, v => v.Value);
var MyXDocument = new XElement("ROOT",
new XElement("ERRORS")
);
var sw = Stopwatch.StartNew();
//===
var errorTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff");
var addedIndex = MyInfoCollection.Select((item, index) =>
new
{
Value = item.Value,
Key = item.Key,
Index = index
});
var errorQuery = from item in addedIndex
where string.IsNullOrEmpty(item.Value)
let sequenceNumber = item.Key + 1
let detail = "No information was read for expected " +
"sequence number " + sequenceNumber
select new XElement("ERROR",
new XElement("DATETIME", errorTime),
new XElement("DETAIL", detail),
new XAttribute("TYPE", "MISSED"),
new XElement("PAGEID", sequenceNumber)
);
var errors = MyXDocument.Descendants("ERRORS").FirstOrDefault();
if (errors != null)
errors.Add(errorQuery);
//===
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); //623
}
static RandomNumberGenerator rand = RandomNumberGenerator.Create();
static int MoreRandom()
{
var buff = new byte[1];
rand.GetBytes(buff);
return buff[0];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.