[英]How can I speed up DataColumn check?
string specifiedSymbol = "MySymbol";
Systems.Data.DataTable table = CreateInitialTable();
if (!table.Columns.Contains(specifiedSymbol))
table.Columns.Add(specifiedSymbol, Type.GetType("System.Double"));
I have just profiled the above statment and it seems that the IF takes around 10ms. 我刚刚描述了上述陈述,看来中频大约需要10毫秒。 This is a long time for my app. 这对我的应用来说是很长的时间。 How else could I check if a column exists in datatable more efficiently? 我还能如何更有效地检查数据表中是否存在列?
It's not obvious what classes or imports you are using, so I will provide a generic answer. 您正在使用什么类或导入尚不清楚,因此我将提供一个通用答案。
When calling Contains()
on a collection which has O(n) complexity (or worse), performance can be improved by doing one of the following: 在具有O(n)复杂度(或更糟)的集合上调用Contains()
时,可以通过执行以下任一操作来提高性能:
HashSet
in which you mirror any changes made to the O(n) collection, then use the Contains()
-method of the HashSet
to test for presence. 管理一个单独的HashSet
在其中镜像对O(n)集合所做的任何更改,然后使用HashSet
的Contains()
方法测试是否存在。 HashSet
and filling it with the contents of the collection every time you need to call Contains()
iff you need to call it multiple times in a row, ie without intermittent change-operations and the time lost using the collection's implementation taking longer than creating a new HashSet
from its contents entirely. 如果您无法跟踪对集合的更改,则您仍然可以通过创建一个HashSet
并在每次需要调用Contains()
时用集合的内容填充它来提高性能, 前提是您需要在一个容器中多次调用它。行,即没有间歇的更改操作,并且使用集合的实现所花费的时间要比完全根据其内容创建一个新的HashSet
花费的时间更长。 This is less desirable than the former for performance reasons, but more desirable for code-manageability. 由于性能原因,这比前者更不可取,但对代码可管理性则更可取。 The best case scenario is being able to write a new class which inherits from the collection's class and putting all the logic in there. 最好的情况是能够编写一个新类,该类继承自集合的类,并将所有逻辑放入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.