Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting powershell排序非常大的对象集合_Sorting_Powershell_Collections - Fatal编程技术网

Sorting powershell排序非常大的对象集合

Sorting powershell排序非常大的对象集合,sorting,powershell,collections,Sorting,Powershell,Collections,我试图从一个非常大的对象集合(-GT250K)生成直方图。我需要根据每个对象的属性对集合进行排序。我的脚本行如下所示: $ch = $ch | sort TotalCount -descending <br> $ch=$ch |排序总计数-降序 其中,$ch[x].totalcount将是某个整数 该脚本可以正常工作,但排序需要一个多小时,并消耗6GB内存。我如何加快这个过程 我已经搜索了一些解决方案,一些网站建议使用[array]::排序,因为它更快。由于这是一组对象,我不确

我试图从一个非常大的对象集合(-GT250K)生成直方图。我需要根据每个对象的属性对集合进行排序。我的脚本行如下所示:

$ch = $ch | sort TotalCount -descending  <br>
$ch=$ch |排序总计数-降序
其中,
$ch[x].totalcount
将是某个整数

该脚本可以正常工作,但排序需要一个多小时,并消耗6GB内存。我如何加快这个过程

我已经搜索了一些解决方案,一些网站建议使用[array]::排序,因为它更快。由于这是一组对象,我不确定如何使用静态
System.Array
排序方法。即使可以,我也不知道如何使数组降序(尽管反转结果应该非常简单)


关于如何使用powershell对大型集合进行排序,有什么建议吗?

让我们创建一个包含2500个元素的数组。数组的每个元素都是一个包含属性
totalCount
的对象,我们为其分配一个整数

$array = @()
1..2500 | % {
    $array += New-Object pscustomobject -Property @{
        totalCount = $_;
    }
}
现在,让我们对该数组进行排序,并测量执行该命令的总时间

我们从经典的
排序对象
开始,使用
-降序
参数:

(Measure-Command {
    $array = $array | Sort-Object TotalCount -descending
}).TotalSeconds  
总时间(以秒为单位):0.1217965

现在让我们使用类System.Array的方法Reverse:
[Array]::Reverse()

总时间(以秒为单位):0.0002594

完全不同

现在让我们看看其他的可能性,让我们创建一个
System.Collections.ArrayList

$array = New-Object System.Collections.ArrayList
1..2500 | % {
    $o = New-Object pscustomobject -Property @{
        totalCount = $_;
    }
    [Void] $array.Add($o)
}
然后我们重复。我们首先使用类System.Collections.ArrayList的反向方法,然后将集合传递给System.Array的反向方法

(Measure-Command {
    $array.reverse()
}).TotalSeconds
总时间(以秒为单位):0.0002459

稍有改善,但总体上相当相似

现在我们键入系统集合并使用
[Array]::Reverse()

总时间(以秒为单位):0.0008172 超过两倍的时间。这清楚地表明这不是一个好主意,所以我们放弃了它

结论:


带有
[Array]::Reverse()
的System.Array肯定比
排序对象
快,但是请记住System.Array是不可变的,因此如果构建数组是性能问题的一部分,我绝对建议使用
System.Collections.ArrayList,因为它是可变的

[array]::reverse()没有以任何方式对数组进行排序。

如果希望快速排序,请执行以下操作:测量命令{$array=New Object System.Collections.ArrayList 1..2500 |%{[Void]$array.Add([tuple]::create([int]$))}$array.sort()}
(Measure-Command {
    $array.reverse()
}).TotalSeconds
(Measure-Command {
    [Array]::Reverse([array]$array)
}).TotalSeconds