将 90000 个 XElement 添加到 XDocument
我有一个 Dictionary
它包含 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 分钟才能完成。我似乎找不到瓶颈可能在哪里,或者这个时机听起来是否合适?
任何人都可以看出为什么要花这么长时间吗?
I have a Dictionary<int, MyClass>
It contains 100,000 items
10,000 items value is populated whilst 90,000 are null.
I have this code:
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)
)
);
}
}
This seems to take about 2 minutes to complete. I can't seem to find where the bottleneck might be or if this timing sounds about right?
Can anyone see anything to why its taking so long?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的
MyInfoCollection
很大,我不会对其调用ToList()
,以便您可以使用ForEach
扩展方法。调用 ToList() 将创建并填充一个巨大的列表。我将删除ToList()
调用,并将.ForEach
放入foreach
语句中,或者编写一个 .ForEach
IEnumerable
的扩展方法。然后对其进行分析并查看需要多长时间。另一件要做的事情是删除
ERRORS
元素的查找和 null 检查。如果不存在,则不要调用上面的foreach
语句。这样你就可以对它进行一次 null 检查,而不是 90,000 次。另外,正如 Michael Stum 指出的那样,我定义一个字符串来保存值
DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")
,然后引用它或另外,您甚至不使用此调用:If your
MyInfoCollection
is huge, I wouldn't callToList()
on it just so you can use theForEach
extension method. CallingToList()
is going to create and populate a huge list. I'd remove theToList()
call, and make the.ForEach
into afor each
statement, or write a .ForEach
extension method forIEnumerable<T>
.Then profile it and see how long it takes. One other thing to do is remove the find and null check of the
ERRORS
element. If it's not there, then don't call thefor each
statement above. That way you null check it one time instead of 90,000 times.Plus as Michael Stum pointed out, I'd define a string to hold the value
DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")
, then reference it or pass it in. Plus, you don't even use this call:这是我最有可能做的事情。
This is what I would most likely do.
用 LINQ 查询替换方法调用怎么样?
How about replacing the method call with a LINQ query?