在 ArrayList 中搜索对象的最优化方法
我有一个 IP 列表(列表“X”),我正在尝试查找其域名。 我还有一个 Excel 文件,其中包含 170 000 多个 IP 条目以及相应的域名。
首先我导入 Excel 文件: $dnsEntries = Import-Excel -Path '.\file.xlsx' -WorksheetName "list"
然后对于列表“X”中的每个 IP,我尝试在 $dnsEntries< 中找到它/code>,这样我就可以获取域名:
foreach($IP in $IPs) {
$DN=($dnsEntries.Where({$_."Address" -like $IP})).Name
}
不幸的是,每个IP至少需要2秒,而我有5000+,所以这需要很长的时间。令我困扰的是,Excel 可以通过一个简单的公式在不到 3 秒的时间内找到所有 5000 多个相应的域名。我知道我永远无法如此接近 PowerShell,但差异怎么会如此之大呢?
有什么办法可以加快搜索速度吗?
预先非常感谢!
编辑:这是一个可重现的示例。有些 IP 不会匹配。
$IPs = @("10.10.10.1","10.10.10.5","10.10.10.66","10.10.10.99","10.10.10.235","10.10.10.73","10.10.10.98","10.10.10.56")
$dnsEntries = @(
@{Address="10.10.10.73";Name="ip1.local"},
@{Address="10.10.10.98";Name="ip2.local"},
@{Address="10.10.10.1";Name="ip3.local"},
@{Address="10.10.10.56";Name="ip4.local"},
@{Address="10.10.10.235";Name="ip5.local"},
@{Address="10.10.10.5";Name="ip6.local"},
@{Address="10.10.10.11";Name="ip7.local"},
@{Address="10.10.10.81";Name="ip8.local"},
@{Address="10.10.10.2";Name="ip9.local"}
)
foreach($IP in $IPs) {
$DN=($dnsEntries.Where({$_."Address" -like $IP})).Name
Write-Host $DN
}
I have a list of IPs (list "X") for which I'm trying to find the domain name.
I also have an Excel file containing 170 000+ entries of IP with the corresponding domain name.
So first I import the Excel file:$dnsEntries = Import-Excel -Path '.\file.xlsx' -WorksheetName "list"
And then for each IP in list "X", I try to find it in $dnsEntries
, so I can get the domain name:
foreach($IP in $IPs) {
$DN=($dnsEntries.Where({$_."Address" -like $IP})).Name
}
Unfortunately, this takes at least 2 seconds for each IP, and I have 5000+, so this takes a loong time. What bothers me is that Excel can find ALL 5000+ corresponding domain names in less than 3 seconds with a simple formula. I understand that I could never come this close with PowerShell, but how the difference could be so big?
Is there any way I could speed up the search?
Thanks a lot in advance!
Edit: Here is a reproducible example. Some IPs won't be matched.
$IPs = @("10.10.10.1","10.10.10.5","10.10.10.66","10.10.10.99","10.10.10.235","10.10.10.73","10.10.10.98","10.10.10.56")
$dnsEntries = @(
@{Address="10.10.10.73";Name="ip1.local"},
@{Address="10.10.10.98";Name="ip2.local"},
@{Address="10.10.10.1";Name="ip3.local"},
@{Address="10.10.10.56";Name="ip4.local"},
@{Address="10.10.10.235";Name="ip5.local"},
@{Address="10.10.10.5";Name="ip6.local"},
@{Address="10.10.10.11";Name="ip7.local"},
@{Address="10.10.10.81";Name="ip8.local"},
@{Address="10.10.10.2";Name="ip9.local"}
)
foreach($IP in $IPs) {
$DN=($dnsEntries.Where({$_."Address" -like $IP})).Name
Write-Host $DN
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其中一条评论中所述,我使用以下代码将
$dnsEntries
从 ArrayList 转换为 Hashtable:转换它仍然需要很长时间,但是然后寻找一个域名IP几乎是即时的!所以最终节省了很多时间。
感谢大家的建议!
As stated in one of the comments, I converted the
$dnsEntries
from an ArrayList to a Hashtable with the following code:It still takes a long time to convert it, but then looking for a domain name for an IP is almost instant! So in the end it saves a lot of time.
Thanks to all of you for your advice!