PowerShell 性能差异过滤器与函数

Mar*_*nus 11 powershell

我目前正在阅读 Windows PowerShell 3.0 Step by Step 书,以获取有关 PowerShell 的更多见解。

在第 201 页上,作者演示了过滤器比具有相同功能的函数更快。

这个脚本在他的电脑上需要 2.6 秒:

MeasureAddOneFilter.ps1
Filter AddOne
{ 
 "add one filter"
  $_ + 1
}

Measure-Command { 1..50000 | addOne }
Run Code Online (Sandbox Code Playgroud)

而这个 4.6 秒

MeasureAddOneFunction.ps1
Function AddOne
{  
  "Add One Function"
  While ($input.moveNext())
   {
     $input.current + 1
   }
}

Measure-Command { 1..50000 | addOne }
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码,则会得到与他的结果完全相反的结果:

.\MeasureAddOneFilter.ps1
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 226
Ticks             : 2266171
TotalDays         : 2,62288310185185E-06
TotalHours        : 6,29491944444444E-05
TotalMinutes      : 0,00377695166666667
TotalSeconds      : 0,2266171
TotalMilliseconds : 226,6171

.\MeasureAddOneFunction.ps1

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 93
Ticks             : 933649
TotalDays         : 1,08061226851852E-06
TotalHours        : 2,59346944444444E-05
TotalMinutes      : 0,00155608166666667
TotalSeconds      : 0,0933649
TotalMilliseconds : 93,3649
Run Code Online (Sandbox Code Playgroud)

谁可以给我解释一下这个?

Rya*_*ies 13

除非作者给出更多的支持证据,否则他可能只是满嘴热气。你已经运行了测试并得到了结果并证明他错了。

编辑:来自 Jeffrey Snover 的博客:

过滤器是一个只有进程脚本块的函数

鉴于两者具有相同的进程块,仅凭这一点还不足以让我相信过滤器将比函数具有速度优势。

还有那个人在 4.6 秒内给一个数字加一是什么 1950 年代的设备?

PS C:\Users\Ryan> Measure-Command { Filter AddOne { $_ + 1 }; AddOne 1 }

TotalMilliseconds : 7.7266


PS C:\Users\Ryan> Measure-Command { Function AddOne { $_ + 1 }; AddOne 1 }    

TotalMilliseconds : 0.4108
Run Code Online (Sandbox Code Playgroud)

4.6 秒是重击。也许作者在生成二进制文件之前使用了某种 CTP 版本的 Powershell。:P

最后,在新的 Powershell 会话中尝试您的测试,但顺序相反。先尝试函数,然后再尝试过滤器,反之亦然:

PS C:\Users\Ryan> Measure-Command { Function AddOne { $_ + 1 }; AddOne 1 }    

TotalMilliseconds : 6.597    


PS C:\Users\Ryan> Measure-Command { Filter AddOne { $_ + 1 }; AddOne 1 }

TotalMilliseconds : 0.4055
Run Code Online (Sandbox Code Playgroud)

看?您运行的第一个总是较慢。这只是关于已经将内容加载到内存中的 .NET 内部结构,这使得第二个操作更快,无论它是函数还是过滤器。

我承认尽管函数似乎始终比过滤器快,无论它运行多少次。

Measure-Command { Function AddOne($Num) { Return $Num += 1 }; 1..50000 | AddOne $_ }

TotalMilliseconds : 13.9813

Measure-Command { Filter AddOne($Num) { Return $Num += 1 }; 1..50000 | AddOne $_ }

TotalMilliseconds : 69.5301
Run Code Online (Sandbox Code Playgroud)

所以作者错了……现在我并不为以前从未使用过过滤器而不是函数而感到难过。


小智 6

实际上,如果在两个测试中使用相同的 $_ ,差异会小得多。我没有调查原因,但我想这是因为作者在两个测试中没有使用相同的方法。此外,控制台输出可能会干扰结果。如果你切割这些部分,数字非常相似。看:

Function AddOneFunction
{  
    process {
        $_ + 1
    }
}

Filter AddOneFilter
{ 
    $_ + 1
}

write-host "First"
Measure-Command { 1..50000 | AddOneFilter } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFilter } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFilter } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFilter } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFilter } | select totalMilliseconds

write-host "Second"
Measure-Command { 1..50000 | AddOneFunction } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFunction } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFunction } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFunction } | select totalMilliseconds
Measure-Command { 1..50000 | AddOneFunction } | select totalMilliseconds
Run Code Online (Sandbox Code Playgroud)

即使您更改命令的顺序,结果也将非常接近。

First

TotalMilliseconds
-----------------
        84.6742
        84.7646
        89.8603
        82.3399
        83.8195
Second
        86.8978
        87.4064
        89.304
        94.4334
        87.0135
Run Code Online (Sandbox Code Playgroud)

该文档还指出,过滤器基本上是仅包含进程块的函数的快捷方式。除非使用进程块(或其他一些技术,例如使用自动变量,例如 $input)指定,否则函数将运行一次,不使用输入,也不传递到管道中的下一个命令。

更多信息请访问https://technet.microsoft.com/en-us/library/hh847829.aspxhttps://technet.microsoft.com/en-us/library/hh847781.aspx