我在通用列表中用于检索的方法是否已优化

发布于 2024-09-04 10:32:25 字数 1135 浏览 4 评论 0原文

有时会有大量的记录,大约有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仄言 2024-09-11 10:32:26

不,事实并非如此。您的整个构造无法在多任务环境中使用。您正在存储要作为类的实例成员进行搜索的详细信息。我会利用 PLINQ(并行 Linq)和常用运算符,我也不会从列表本身派生,但提供这样的扩展方法:

public static EquipStatus GetEquipmentRecord(this IEnumerable<EquipStatus> list, int equipmentID, EquipShift shift, DateTime date)
{
  return list.AsParallel().FirstOrDefault(e => e.EquipmentID == equipmentID && e.Shift == shift, e.Date.Date == date.Date);
}

通过这种方式,可以同时进行多个搜索。

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:

public static EquipStatus GetEquipmentRecord(this IEnumerable<EquipStatus> list, int equipmentID, EquipShift shift, DateTime date)
{
  return list.AsParallel().FirstOrDefault(e => e.EquipmentID == equipmentID && e.Shift == shift, e.Date.Date == date.Date);
}

By this, multiple searches at the same time are possible.

云醉月微眠 2024-09-11 10:32:26

改进 checkForEquipRecord 方法的一个明显方法是更改

if ((equip.Date.Date == _date.Date) &&  (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)  )
    return true;
else
    return false;

​​为 return (equip.Date.Date == _date.Date) && (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)

就效率而言,它可能已经是 JIT 编译器所做的优化。

well an obvious way to improve your checkForEquipRecord method is to change

if ((equip.Date.Date == _date.Date) &&  (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)  )
    return true;
else
    return false;

to 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.

只是偏爱你 2024-09-11 10:32:25

在不评论您选择的算法的情况下,我们可以说它可能已经足够优化

你有一个 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.

貪欢 2024-09-11 10:32:25

您可以通过实现合适的 GetHashCode 方法并使用 System.Collections.Generic.HashSet 作为后备容器来显着加快速度。但是,由于尚不完全清楚您如何使用您的类(即您使用哪些其他 List 方法),因此 ymmv.

You could speed this up considerably by implementing a suitable GetHashCode method and using a System.Collections.Generic.HashSet<EquipStatus> as the backing container. However, as it's not entirely clear how you are using your class (i.e. which other List<T> methods you use), ymmv.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文