低效的SelectSingleNode和高效的ListView数据添加

来源:互联网 发布:淘宝客服售后技巧话术 编辑:程序博客网 时间:2024/04/29 04:01
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

以下Santomania带给我们的一些提高代码性能的建议,在他的博客网站上,你能看到不少他关于SharePoint的见解。

一、低效SelectSingleNode

我最近一直在使用reflecting,然而我确实已经通过Lamont Harrington对此做了提示,虽然对我来说这并不是一个新闻(一般说来,在木桶中,我不会是最慢的鱼^_^),但我发现它仍然值得一提。

反编译XmlNode.SelectSingleNode方法,你将获得如下结果(原谅我的VB.Net):

Public Overloads Function SelectSingleNode(ByVal xpath As String) As XmlNode

Begin Function

Dim list1 As XmlNodeList
Dim node1 As XmlNode
Try

list1 = Me.SelectNodes(xpath)
node1 = list1.ItemOf(0)

Catch ArgumentOutOfRangeException

node1 = Nothing

End Try
Return node1

End Function

       你将会看到,SelectSingleNode仅仅是SelectNodes的包装,这意味着它(最快)不会比SelectNode更快,并且,如果你在深入研究一点,SelectNodes反编译后如下:

Public Overloads Function SelectNodes(ByVal xpath As String) As XmlNodeList
Begin Function

im navigator1 As XPathNavigator
navigator1 = Me.CreateNavigator
Return New XPathNodeList(navigator1.Select(xpath))

End Function

   让人感到沮丧痛苦的是:SelectNodes确实是创造了一个XPathNavigator实例!!

   我想,首选的解决方案是:实例化我们自己的XPathNavigator并且在对xml文档执行XPath查询的时候重复使用它,特别是我们执行嵌套循环的查询时。

                                   二、高效ListView数据添加

       怎样促进你的ListView的性能?不要使用在同一个的循环中去创造和添加ListViewItem的方法。更好的方法是:创造一个ListViewItem的集合,并且使用AddRange方法去添加它们。我显示这个方法对团队中的一个开发者,在大量目录添加的情况下(超过1500条记录),他的ListView的性能比原来要高效1000倍。

      

       最后,不知你注意到了没有,DictionaryEntry不是一个类,而是一个结构。

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击