在 PowerShell 中忽略输出的更好(更干净)方法是什么? [英] What's the better (cleaner) way to ignore output in PowerShell?

查看:69
本文介绍了在 PowerShell 中忽略输出的更好(更干净)方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个方法或 cmdlet 可以返回某些内容,但您不想使用它,也不想输出它.我找到了这两种方法:

Let's say you have a method or a cmdlet that returns something, but you don't want to use it and you don't want to output it. I found these two ways:

Add-Item > $null

[void]Add-Item

Add-Item | Out-Null

你用什么?哪个是更好/更清洁的方法?为什么?

What do you use? Which is the better/cleaner approach? Why?

推荐答案

我只是对我所知道的四个选项进行了一些测试.

I just did some tests of the four options that I know about.

Measure-Command {$(1..1000) | Out-Null}

TotalMilliseconds : 76.211

Measure-Command {[Void]$(1..1000)}

TotalMilliseconds : 0.217

Measure-Command {$(1..1000) > $null}

TotalMilliseconds : 0.2478

Measure-Command {$null = $(1..1000)}

TotalMilliseconds : 0.2122

## Control, times vary from 0.21 to 0.24
Measure-Command {$(1..1000)}

TotalMilliseconds : 0.2141

因此,由于开销,我建议您使用除 Out-Null 以外的任何内容.对我来说,下一个重要的事情是可读性.我有点喜欢重定向到 $null 并设置等于 $null 自己.我过去更喜欢强制转换为 [Void],但在浏览代码或新用户时,这可能不太好理解.

So I would suggest that you use anything but Out-Null due to overhead. The next important thing, to me, would be readability. I kind of like redirecting to $null and setting equal to $null myself. I use to prefer casting to [Void], but that may not be as understandable when glancing at code or for new users.

我想我更喜欢将输出重定向到 $null.

I guess I slightly prefer redirecting output to $null.

Do-Something > $null

编辑

在 stej 再次发表评论后,我决定对管道进行更多测试,以更好地隔离破坏输出的开销.

After stej's comment again, I decided to do some more tests with pipelines to better isolate the overhead of trashing the output.

以下是一些使用简单的 1000 个对象管道进行的测试.

Here are some tests with a simple 1000 object pipeline.

## Control Pipeline
Measure-Command {$(1..1000) | ?{$_ -is [int]}}

TotalMilliseconds : 119.3823

## Out-Null
Measure-Command {$(1..1000) | ?{$_ -is [int]} | Out-Null}

TotalMilliseconds : 190.2193

## Redirect to $null
Measure-Command {$(1..1000) | ?{$_ -is [int]} > $null}

TotalMilliseconds : 119.7923

在这种情况下,Out-Null 有大约 60% 的开销,>$null 有大约 0.3% 的开销.

In this case, Out-Null has about a 60% overhead and > $null has about a 0.3% overhead.

附录 2017-10-16: 我最初忽略了 Out-Null 的另一个选项,即 -inputObject 参数的使用.使用这个开销似乎消失了,但是语法不同:

Addendum 2017-10-16: I originally overlooked another option with Out-Null, the use of the -inputObject parameter. Using this the overhead seems to disappear, however the syntax is different:

Out-Null -inputObject ($(1..1000) | ?{$_ -is [int]})

现在使用简单的 100 个对象管道进行一些测试.

And now for some tests with a simple 100 object pipeline.

## Control Pipeline
Measure-Command {$(1..100) | ?{$_ -is [int]}}

TotalMilliseconds : 12.3566

## Out-Null
Measure-Command {$(1..100) | ?{$_ -is [int]} | Out-Null}

TotalMilliseconds : 19.7357

## Redirect to $null
Measure-Command {$(1..1000) | ?{$_ -is [int]} > $null}

TotalMilliseconds : 12.8527

这里再次 Out-Null 有大约 60% 的开销.虽然 <代码>>$null 有大约 4% 的开销.此处的数字因测试而异(我每次都跑了大约 5 次并选择了中间地带).但我认为它显示了一个不使用 Out-Null 的明确理由.

Here again Out-Null has about a 60% overhead. While > $null has an overhead of about 4%. The numbers here varied a bit from test to test (I ran each about 5 times and picked the middle ground). But I think it shows a clear reason to not use Out-Null.

这篇关于在 PowerShell 中忽略输出的更好(更干净)方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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