我可以加快此查询检索域中的所有计算机?计算机

由网友(朝夕盼兮)分享简介:我写了一个辅助类,以获得一个域中的所有计算机,但它是一个稍微有点慢。虽然有回到128的对象,我仍希望加速这一过程。任何想法?公共类DomainBrowser{私人常量字符串电脑=计算机;公共字符串域{获得;私定; }公共DomainBrowser(字符串域){this.Domain = domain.ToLower(...

我写了一个辅助类,以获得一个域中的所有计算机,但它是一个稍微有点慢。虽然有回到128的对象,我仍希望加速这一过程。任何想法?

 公共类DomainBrowser
{
    私人常量字符串电脑=计算机;

    公共字符串域{获得;私定; }

    公共DomainBrowser(字符串域)
    {
        this.Domain = domain.ToLower();
    }

    ///<总结>
    ///这个方法返回当前域中可用计算机名称的列表。
    ///< /总结>
    ///<返回>< /回报>
    公开名单<字符串> GetComputers()
    {
        VAR winDirEntries =新的DirectoryEntry(WINNT:);

        VAR电脑=(从winDirEntries.Children的DirectoryEntry领域
                         其中domain.Name.ToLower()== this.Domain
                         从PC的DirectoryEntry在domain.Children
                         其中,pc.SchemaClassName.ToLower()。包含(计算机)
                         选择pc.Name).ToList();

        回到电脑;
    }
}
 

解决方案

一个最大的问题,这里是所有的的TOLOWER()通话。字符串是不变的,所以他们的每一个变化(如改变他们小写在您的code示例所示)创建了一个新的对象。此外,参与lowercasing字符串中的逻辑是比你想象的更昂贵,因为它必须考虑到像当前区域性设置。

要减少为代价的,尽量不要改变字符串的参考,而不是将它们与不区分大小写比较:

  VAR电脑=(从的DirectoryEntry域winDirEntries.Children
                     其中,string.Equals(domain.Name,this.Domain,StringComparison.OrdinalIgnoreCase)
                     从PC的DirectoryEntry在domain.Children
                     其中,pc.SchemaClassName.IndexOf(计算机,StringComparison.OrdinalIgnoreCase)!= -1
                     选择pc.Name).ToList();
 
如何快速查找 检索 电脑中的文件

请注意,我不得不改变的String.Compare string.IndexOf ,因为比较不具有与不区分大小写工作过载。

I wrote a helper class to get all computers on a domain, but it's a tad bit slow. While there are 128 objects returned, I'd still like to speed it up. Any ideas?

public class DomainBrowser
{
    private const string Computer = "computer";

    public string Domain { get; private set; }

    public DomainBrowser(string domain)
    {
        this.Domain = domain.ToLower();
    }

    /// <summary>
    /// This method returns a list of the computer names available in the current domain.
    /// </summary>
    /// <returns></returns>
    public List<string> GetComputers()
    {
        var winDirEntries = new DirectoryEntry("WinNT:");

        var computers = (from DirectoryEntry domain in winDirEntries.Children
                         where domain.Name.ToLower() == this.Domain
                         from DirectoryEntry pc in domain.Children
                         where pc.SchemaClassName.ToLower().Contains(Computer)
                         select pc.Name).ToList();

        return computers;
    }
}

解决方案

One of the biggest issues here is all of the ToLower() calls. Strings are immutable, so each change of them (such as changing them to lowercase as seen in your code sample) creates a new object. Additionally, the logic involved in lowercasing a string is more expensive than you might think, because it has to account for things like the current culture settings.

To mitigate the expense, try not altering the string references, instead comparing them with case insensitivity:

var computers = (from DirectoryEntry domain in winDirEntries.Children
                     where string.Equals(domain.Name, this.Domain, StringComparison.OrdinalIgnoreCase)
                     from DirectoryEntry pc in domain.Children
                     where pc.SchemaClassName.IndexOf(Computer, StringComparison.OrdinalIgnoreCase) != -1
                     select pc.Name).ToList();

Note that I had to change string.Compare to string.IndexOf because Compare does not have an overload that works with case insensitivity.

阅读全文

相关推荐

最新文章