性能 - Microsoft.Office.Interop.Outlook.Items.Find() 与 LINQ

Performance - Microsoft.Office.Interop.Outlook.Items.Find() vs LINQ

我的公司有点担心以下两种从 Outlook 获取约会的方式可能存在性能差异:

private Microsoft.Office.Interop.Outlook.AppointmentItem FindeAppointment(DateTime startzeit, string subject)
{
    string filter = string.Format("[Start] = '{0}' AND [Subject] = '{1}' AND [BillingInformation] = 'TEST'", startzeit.ToShortDateString() + " " + startzeit.ToShortTimeString(), subject);
    MAPIFolder calendarFolder = outlookApplication.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
    Items items = calendarFolder.Items;
    return items.Find(filter) as AppointmentItem;
}

private AppointmentItem FindeAppointment(DateTime startzeit, string subject)
{
    MAPIFolder calendarFolder = m_outlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
    IEnumerable<AppointmentItem> items = calendarFolder.Items.OfType<AppointmentItem>();
    return items.FirstOrDefault(p => p.Start == startzeit &&
                                        p.Subject == subject &&
                                        p.BillingInformation == "TEST");
}

我们想用 LINQ 的方式来做,因为我们最近在过滤器上遇到了一些问题,所以我们需要一个解决方法。

我不知道有什么好的方法可以测试这些方法的性能,我不想创建数以千计的约会。

所以,任何人有一些数字或者可以告诉我什么是更快或至少,如何测试它?

或者我们不应该关心使用什么,因为它的速度非常低?

这是推测,但如果发现 Outlook.Items.Find 更糟,我一点也不会感到惊讶,因为它确实利用了 Exchange 中的索引,而不是将所有项目放入内存并通过 Linq 迭代它们。

how to test it?

只有 才能确定它可以为您的环境获取具有代表性的示例并测试两种搜索方式。如果您的生产环境还不是很大,但您 "worried" 它可能 变得 大,那可能意味着在测试 Exchange 环境中创建 "thousands of appointments"。好消息是您已经知道如何使用 API,因此创建一堆随机约会应该没什么大不了的。 :)

对 OOM 对象使用 LINQ 语句并不是一个好主意。事实上,底层 COM 对象未被释放。如果您不及时释放这些对象,您可能会达到 Exchange 对任何一次打开的最大项目数的限制。如果您的加载项试图在存储在 Microsoft Exchange Server 上的集合中枚举超过 256 个 Outlook 项目,这一点尤其重要。

使用 System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects 篇文章。

我还注意到您在单行代码中使用了多个点:

outlookApplication.Session.GetDefaultFolder

应用程序的Session 属性 class returns 命名空间的实例class 应在之后发布。在这种情况下,您可能会看到对象在被释放时不受控制。所以,我总是建议打破 属性 和方法调用的链条,并在单独的代码行上声明它们。

最后,Find/FindNext 或 Restrict 方法不会像 LINQ 那样将项目加载到内存中。所以,他们会快得多。您可以在以下文章中阅读有关这些方法的更多信息: