我可以加快此查询检索域中的所有计算机? [英] Can I speed up this query to retrieve all computers on the domain?

查看:126
本文介绍了我可以加快此查询检索域中的所有计算机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个辅助类,以获得一个域中的所有计算机,但它是一个稍微有点慢。虽然有回到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.

这篇关于我可以加快此查询检索域中的所有计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆