Powershell Merge 2列出了性能 [英] Powershell Merge 2 lists Performance

查看:135
本文介绍了Powershell Merge 2列出了性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Powershell中有两个包含大量数据的列表:

I have two lists in Powershell with a large amount of data:

  • $ Byods包含大约5000项的MAC和用户名
  • $ DHCP租约,其中包含MAC和IP约3000项

我想创建一个新列表,其中包含Byods列表在前的用户名和IP,并从DHCPleases中找到IP,仅包含找到匹配项的记录(左联接?)

I want to create a new list containing Usernames and IPs where the Byods list is leading, and the IPs are found from the DHCPleases, only containg records that found a match (a left join?)

我创建了一个foreach循环,即可完成工作.但是,这需要大量时间才能完成(> 30分钟).

I created a foreach loop, that does the job. However, its taking a huge ammount of time to complete (> 30 min).

我确信这可以更快.有人吗?

Im sure this can be faster. Anyone?

$UserByods = @()
foreach ($lease in $DHCPLeases) 
{
    $MAC = [string]$lease.MAC
    $UserByod = @()
    $UserByod = $Byods | where {$_.MAC -eq $MAC}
    if (($UserByod | measure).count -eq 1) {
        $ByodIP = New-Object -TypeName PSObject
        $ByodIP | Add-Member -Name 'User' -MemberType Noteproperty -Value $UserByod.Username
        $ByodIP | Add-Member -Name 'IP' -MemberType Noteproperty -Value $lease.IP
        $UserByods += $ByodIP
    }
}

推荐答案

可以在此处进行许多改进.首先,不要使用Add-Member构造对象,这比预先指定属性要慢得多.

A number of improvements can be made here. First off, don't use Add-Member to construct the objects, it'll be significantly slower than specifying the properties up front.

您还希望避免在集合上使用加法运算符(+=),因为这将导致调整基础数组的大小,这是一项占用大量内存的操作.

You'd also want to avoid using the addition operator (+=) on an collection, since it'll cause the underlying array to be resized which is a quite memory-intensive operation.

最后使用哈希表进行MAC相关,它比循环遍历所有5000个条目3000次要快得多(这基本上是...| Where {...}的工作):

Finally use a hashtable for the MAC correlation, it'll be much faster than looping through all 5000 entries 3000 times (which is what ...| Where {...} basically does):

$BYODTable = $Byods |ForEach-Object -Begin { $table = @{} } -Process { $table[$_.MAC] = $_.Username } -End { return $table }

$UserByods = foreach ($lease in $DHCPLeases) 
{
    $MAC = [string]$lease.MAC
    if ($BYODTable.ContainsKey($MAC)) {
        New-Object -TypeName PSObject -Property @{
            User = $BYODTable[$MAC]
            IP   = $lease.IP
        }
    }
}

这篇关于Powershell Merge 2列出了性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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