假设您有一个方法或cmdlet返回某些内容,但您不想使用它,也不想输出它。我找到了这两种方法:
Add-Item > $null
[void]Add-Item
Add-Item | Out-Null
你用的是什么?哪种方法更好/更干净?为什么?
发布于 2017-08-09 04:19:12
我知道这是一个旧的线程,但是对于那些把@JasonMArcher的答案当做事实的人来说,我很惊讶它没有被纠正,我们中的许多人多年来都知道这实际上是管道增加了延迟,与它是否是Out-Null无关。事实上,如果你运行下面的测试,你会很快发现,多年来我们都认为它更快的对void和$void=的“更快”强制转换,当你添加任何流水线时,实际上是同样慢,而且实际上非常慢。换句话说,一旦您通过管道连接到任何内容,不使用out-null的整个规则就会被扔进垃圾桶。
证明,下面列表中的最后3个测试。可怕的Out-null是32339.3792毫秒,但等等-转换为void快了多少? 34121.9251 ms?!?见鬼?在我的系统上,这些是真正的#,转换为VOID实际上要慢一些。如果=$null呢? 34217.685ms.....still更慢!因此,正如最后三个简单的测试所显示的那样,在许多情况下,当管道已经在使用时,Out-Null实际上更快。
那么,这是为什么?很简单。这是,而且一直是100%的幻觉,管道到Out-Null的速度更慢。然而,管道到任何东西的速度都比较慢,我们不是已经通过基本逻辑知道了这一点吗?我们可能不知道有多慢,但这些测试肯定会告诉你使用管道的成本,如果你能避免它的话。而且,我们并不是100%错误的,因为在极少数的真实场景中,out-null是邪恶的。什么时候?添加输出时-Null正在添加唯一的管道活动。在其他words....the原因中,像$(1..1000) | Out-Null这样的简单命令显示为true。
如果你只是在上面的每个测试中添加一个额外的管道来输出字符串,#s就会彻底改变(或者粘贴下面的),正如你自己所看到的,在许多情况下,Out-Null实际上会变得更快:
$GetProcess = Get-Process
# Batch 1 - Test 1
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$GetProcess | Out-Null
}
}).TotalMilliseconds
# Batch 1 - Test 2
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
[void]($GetProcess)
}
}).TotalMilliseconds
# Batch 1 - Test 3
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$null = $GetProcess
}
}).TotalMilliseconds
# Batch 2 - Test 1
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$GetProcess | Select-Object -Property ProcessName | Out-Null
}
}).TotalMilliseconds
# Batch 2 - Test 2
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
[void]($GetProcess | Select-Object -Property ProcessName )
}
}).TotalMilliseconds
# Batch 2 - Test 3
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$null = $GetProcess | Select-Object -Property ProcessName
}
}).TotalMilliseconds
# Batch 3 - Test 1
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$GetProcess | Select-Object -Property Handles, NPM, PM, WS, VM, CPU, Id, SI, Name | Out-Null
}
}).TotalMilliseconds
# Batch 3 - Test 2
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
[void]($GetProcess | Select-Object -Property Handles, NPM, PM, WS, VM, CPU, Id, SI, Name )
}
}).TotalMilliseconds
# Batch 3 - Test 3
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$null = $GetProcess | Select-Object -Property Handles, NPM, PM, WS, VM, CPU, Id, SI, Name
}
}).TotalMilliseconds
# Batch 4 - Test 1
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$GetProcess | Out-String | Out-Null
}
}).TotalMilliseconds
# Batch 4 - Test 2
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
[void]($GetProcess | Out-String )
}
}).TotalMilliseconds
# Batch 4 - Test 3
(Measure-Command {
for ($i = 1; $i -lt 99; $i++)
{
$null = $GetProcess | Out-String
}
}).TotalMilliseconds
发布于 2011-03-10 21:37:10
还有一个可以在管道中使用的Out-Null
cmdlet,例如Add-Item | Out-Null
。
Out-Null手册页
NAME
Out-Null
SYNOPSIS
Deletes output instead of sending it to the console.
SYNTAX
Out-Null [-inputObject <psobject>] [<CommonParameters>]
DETAILED DESCRIPTION
The Out-Null cmdlet sends output to NULL, in effect, deleting it.
RELATED LINKS
Out-Printer
Out-Host
Out-File
Out-String
Out-Default
REMARKS
For more information, type: "get-help Out-Null -detailed".
For technical information, type: "get-help Out-Null -full".
发布于 2011-03-10 21:52:36
我会考虑使用像这样的东西:
function GetList
{
. {
$a = new-object Collections.ArrayList
$a.Add(5)
$a.Add('next 5')
} | Out-Null
$a
}
$x = GetList
不返回来自$a.Add
的输出--这适用于所有$a.Add
方法调用。否则,您需要在每次调用之前预先加上[void]
。
在简单的情况下,我会使用[void]$a.Add
,因为很明显,输出将不会被使用并被丢弃。
https://stackoverflow.com/questions/5260125
复制相似问题