Measure-Command

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

语法

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

说明

Measure-Command cmdlet 在内部运行脚本块或 cmdlet、测量执行操作的时间并返回执行时间。

注意

脚本块由 Measure-Command 在当前作用域中运行,而不是在子作用域中运行。

示例

示例 1:对命令进行测量

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

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

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

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

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

这些命令显示了在 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 提供输入

传递给 Measure-Command 的对象可用于传递给 Expression 参数的脚本块。 该脚本块针对管道上的每个对象执行一次。

# 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

示例 5:测量子作用域内的执行

Measure-Command 在当前作用域中运行脚本块,因此脚本块可以修改当前作用域中的值。 若要避免更改当前作用域,必须将脚本块括在大括号 ({}) 中,并使用调用运算符 (&) 在子作用域中执行该块。

$foo = 'Value 1'
$null = Measure-Command { $foo = 'Value 2' }
$foo
$null = Measure-Command { & { $foo = 'Value 3' } }
$foo

Value 2
Value 2

有关调用运算符的详细信息,请参阅 about_Operators

参数

-Expression

指定要测量时间的表达式。 请将表达式括在大括号 ({}) 中。

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

-InputObject

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

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

输入

PSObject

你可以通过管道将对象传递给此 cmdlet。

输出

TimeSpan

此 cmdlet 返回一个表示结果的时间跨度对象。