如何加速此 Linq 选择代码?
我有一个 xml 文件,每个项目包含 3 条数据:productName、productCode 和 ProductPrice。 xml 文件中的项目数约为 7,000 个。
用户可以通过在文本框中键入“红色衬衫”来按名称搜索产品。 我的代码执行以下任务:
标记搜索文本并收集包含所有搜索词的productName。
按产品代码排序
选择仅显示产品名称和产品价格(不是仅用于 orderby 的产品代码)
var products = from d in xElem.Descendants(fileName) 其中 textBox1.Text.ToLower().Split(' ').All(t => d.Element(productName).Value.ToLower().Contains(t)) orderby d.Element(productCode).Value 升序 选择新的 { 价格 = (double.Parse(d.Element(productPrice).Value)).ToString(numberFormat), 名称 = d.Element(名称).值 };
这段代码的瓶颈在哪里?以及如何去除它们? 每次在文本框中输入按键时,我都会执行此 Linq 选择,这意味着实时结果更新(而不是等待 Enter 键)。 谢谢。
I have an xml file with 3 pieces of data per item: productName, productCode and productPrice. The number of items in the xml file is around 7,000.
User can search for product by name by typing "red shirt" for example in a textbox.
My code does these tasks:
tokenize the search text and collect productName's that contains ALL of the search words.
order the selection by productCode
select for display only productName and productPrice (not productCode which is used only for orderby)
var products = from d in xElem.Descendants(fileName) where textBox1.Text.ToLower().Split(' ').All(t => d.Element(productName).Value.ToLower().Contains(t)) orderby d.Element(productCode).Value ascending select new { Price = (double.Parse(d.Element(productPrice).Value)).ToString(numberFormat), Name = d.Element(Name).Value };
Where in this code are the bottleneck(s)? And how to remove them?
I do this Linq select every time a keystroke is entered in the textbox, meaning realtime result update (vs. waiting for the Enter key).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不附加分析器并查看输出,我只能猜测是什么慢了。
其中 textBox1.Text.ToLower.Split(' ')
可以在查询之外缓存。不过,我怀疑这是一个严重的问题。LINQ to XML 在处理非常大的 XML 文档时可能会很慢。考虑使用
XmlTextReader
以只进方式快速(但丑陋)访问底层 XML。如果 XML 适合,则预处理内存中的数据。内存中的查询应该相当快。
您可以使用诸如 Lucene.Net 之类的东西来索引大量 XML 文件快速搜索。这可能有些过头了,但它非常快且可扩展。
多思考一下这个问题。每次用户按下一个键时,您都会处理 XML DOM 并执行一系列字符串操作。处理 XML 以获得更适合您的任务的表单似乎是一个很好的胜利。
请注意,这种性质的自动完成的响应时间相当宽松,对于大多数用途来说,最多一秒左右的延迟是可以接受的。
Without attaching a profiler and seeing the output I can only guess what is slow.
where textBox1.Text.ToLower.Split(' ')
can be cached outside the query. I doubt this is a serious issue though.LINQ to XML can be slow on very large XML documents. Consider using
XmlTextReader
for fast (but ugly) access to underlying XML in a forward-only manner.Preprocess the data in memory if the XML is amenable to that. In memory queries should be pretty fast.
You can use something like Lucene.Net to index a large quantity of XML files for quick searching. This might be overkill but it's pretty fast and scalable.
Think about the problem a bit more. Every time the user presses a key you're processing the XML DOM and doing a bunch of string operations. Processing the XML such that you have a form more suitable to your task seems like a good win.
Note that the response time for autocomplete of this nature is pretty lax, up to a second of lag or so is pretty acceptable for most purposes.