我在通用列表中用于检索的方法是否已优化
有时会有大量的记录,大约有50,000条。 考虑到这一点,GetEquipmentRecord 方法可以完成该任务。 谢谢你的意见。
c#,net 2,0
public enum EquipShift { day, night };
public class EquipStatusList : List<EquipStatus>
{
string SerialFormat = "yyyyMMdd";
int _EquipmentID;
string _DateSerial;
EquipShift _Shift;
public EquipStatus GetEquipmentRecord(int equipmentID, EquipShift shift,
DateTime date)
{
_DateSerial = date.ToString(SerialFormat);
_Shift = shift;
_EquipmentID = equipmentID;
return this.Find(checkforEquipRecord);
}
bool checkforEquipRecord(EquipStatus equip)
{
if ((equip.EquipmentID == _EquipmentID)
&& (equip.Shift == _Shift)
&& (equip.Date.ToString(SerialFormat) == _DateSerial))
return true;
else
return false;
}
}
更新: 我已将评估更改为阅读,
if ((equip.Date.Date == _date.Date) && (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift) )
不确定是否有帮助
At some time there will be a large amount of records, about 50,000.
with that in mind is the method GetEquipmentRecord up to the task.
thanks for you opinions.
c# ,net 2,0
public enum EquipShift { day, night };
public class EquipStatusList : List<EquipStatus>
{
string SerialFormat = "yyyyMMdd";
int _EquipmentID;
string _DateSerial;
EquipShift _Shift;
public EquipStatus GetEquipmentRecord(int equipmentID, EquipShift shift,
DateTime date)
{
_DateSerial = date.ToString(SerialFormat);
_Shift = shift;
_EquipmentID = equipmentID;
return this.Find(checkforEquipRecord);
}
bool checkforEquipRecord(EquipStatus equip)
{
if ((equip.EquipmentID == _EquipmentID)
&& (equip.Shift == _Shift)
&& (equip.Date.ToString(SerialFormat) == _DateSerial))
return true;
else
return false;
}
}
update :
I have changed the evaluation to read
if ((equip.Date.Date == _date.Date) && (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift) )
not sure it that helps
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,事实并非如此。您的整个构造无法在多任务环境中使用。您正在存储要作为类的实例成员进行搜索的详细信息。我会利用 PLINQ(并行 Linq)和常用运算符,我也不会从列表本身派生,但提供这样的扩展方法:
通过这种方式,可以同时进行多个搜索。
No, it is not. Your whole construct is not able to be used in a multitasking environment. You are storing the details to search for as instance members of the class. I would take advantage of PLINQ (Parallel Linq) and the usual operators, also I wouldn't derive from the List itself, but offer an extension method like this:
By this, multiple searches at the same time are possible.
改进
checkForEquipRecord
方法的一个明显方法是更改为
return (equip.Date.Date == _date.Date) && (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)
就效率而言,它可能已经是 JIT 编译器所做的优化。
well an obvious way to improve your
checkForEquipRecord
method is to changeto just
return (equip.Date.Date == _date.Date) && (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)
As far as efficiency goes, it might already be an optimization that the JIT compiler makes.
在不评论您选择的算法的情况下,我们可以说它可能已经足够优化。
你有一个 O(n)
find()
;使用二分搜索搜索排序列表将是 O(lg n) 并搜索 哈希集(或字典例如,在 C# 2.0 中, 的复杂度为 O(1)。如果您经常调用此函数,哈希集显然是一种可行的方法。但是瓶颈很少出现在您期望的地方,因此您在这个特定实例上提出问题意味着,总的来说,稍后的分析实际上会表明大幅放缓发生在其他地方。
Without commenting on your choice of algorithm, we can say that it probably is optimized enough.
You've got an O(n)
find()
in there; searching a sorted list with a binary search would be O(lg n) and searching a hash-set (or Dictionary in C# 2.0) would be O(1) for example. Hash-set would obviously be the way to go if you were calling this function often.But bottlenecks are rarely where you expect them, so that you ask the question on this particular instance means that, on balance, profiling later will actually show that the big slowdowns are elsewhere.
您可以通过实现合适的
GetHashCode
方法并使用System.Collections.Generic.HashSet
作为后备容器来显着加快速度。但是,由于尚不完全清楚您如何使用您的类(即您使用哪些其他List
方法),因此 ymmv.You could speed this up considerably by implementing a suitable
GetHashCode
method and using aSystem.Collections.Generic.HashSet<EquipStatus>
as the backing container. However, as it's not entirely clear how you are using your class (i.e. which otherList<T>
methods you use), ymmv.