Measure-Command

测量运行脚本块和 cmdlet 所需的时间。

语法

Measure-Command
       [-InputObject <PSObject>]
       [-Expression] <ScriptBlock>
       [<CommonParameters>]

说明

cmdlet Measure-Command 在内部运行脚本块或 cmdlet,对操作的执行进行计时,并返回执行时间。

示例

示例 1:测量命令

此命令测量运行Get-EventLog获取Windows PowerShell事件日志中的事件的命令所需的时间。

Measure-Command { Get-EventLog "windows powershell" }

示例 2:比较 Measure-Command 的两个输出

第一个命令测量处理递归 Get-ChildItem 命令所需的时间,该命令使用 -Path 参数仅获取 C:\Windows 目录及其子目录中 .txt 文件。

第二个命令测量处理使用提供程序特定-Filter参数的递归Get-ChildItem命令所需的时间。

这些命令显示在 PowerShell 命令中使用特定于提供程序的筛选器的值。

Measure-Command { Get-ChildItem -Path C:\Windows\*.txt -Recurse }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 8
Milliseconds      : 618
Ticks             : 86182763
TotalDays         : 9.9748568287037E-05
TotalHours        : 0.00239396563888889
TotalMinutes      : 0.143637938333333
TotalSeconds      : 8.6182763
TotalMilliseconds : 8618.2763

Measure-Command {Get-ChildItem C:\Windows -Filter "*.txt" -Recurse}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 140
Ticks             : 11409189
TotalDays         : 1.32050798611111E-05
TotalHours        : 0.000316921916666667
TotalMinutes      : 0.019015315
TotalSeconds      : 1.1409189
TotalMilliseconds : 1140.9189

示例 3:使用 Measure-Command 的 InputObject 参数

此示例演示如何使用 InputObjectMeasure-Command参数。 ScriptBlock传递给 参数的 Expression 将针对传递的每个对象执行一次,或者通过管道传递到 参数中InputObject

注意

Measure-Command仍然提供传递给 InputObject 参数的每个元素的总体ScriptBlock执行的度量值。

# Perform a simple operation to demonstrate the InputObject parameter
# Note that no output is displayed.
10, 20, 50 | Measure-Command -Expression {for($i=0; $i -lt $_;$i++) {$i} }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 12
Ticks             : 122672
TotalDays         : 1.41981481481481E-07
TotalHours        : 3.40755555555556E-06
TotalMinutes      : 0.000204453333333333
TotalSeconds      : 0.0122672
TotalMilliseconds : 12.2672

示例 4:显示测量的命令的输出

若要在 中 Measure-Command 显示表达式的输出,可以使用管道到 Out-Default

# Perform the same operation as above adding Out-Default to every execution.
# This will show that the ScriptBlock is in fact executing for every item.
10, 20, 50 | Measure-Command -Expression {for($i=0; $i -lt $_;$i++) {$i}; "$($_)" | Out-Default }

10
20
50


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 11
Ticks             : 113745
TotalDays         : 1.31649305555556E-07
TotalHours        : 3.15958333333333E-06
TotalMinutes      : 0.000189575
TotalSeconds      : 0.0113745
TotalMilliseconds : 11.3745

参数

-Expression

指定要测量时间的表达式。 将表达式括在大括号 ({}) 。 参数名称 (“Expression”) 是可选的。

Type:ScriptBlock
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-InputObject

绑定到 参数的对象 InputObject 是传递给参数的 ScriptBlockExpression 的可选输入。 在 ScriptBlock内, $_ 可用于引用管道中的当前对象。

Type:PSObject
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

输入

PSObject

可以通过管道将对象传递给 Measure-Command

输出

TimeSpan

Measure-Command 返回表示结果的时间跨度对象。