Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created March 26, 2012 16:48

Revisions

  1. jpoehls revised this gist Mar 30, 2012. 2 changed files with 22 additions and 7 deletions.
    12 changes: 9 additions & 3 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    PS> time { ping -n 1 google.com } -Samples 10 -Silent
    ..........
    Avg: 00:00:00.0705483
    Min: 00:00:00.0648462
    Max: 00:00:00.1081890
    Avg: 62.1674ms
    Min: 56.9945ms
    Max: 87.9602ms

    PS> time { ping -n 1 google.com } -Samples 10 -Silent -Long
    ..........
    Avg: 00:00:00.0612480
    Min: 00:00:00.0572167
    Max: 00:00:00.0765762
    17 changes: 13 additions & 4 deletions time.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function Measure-Command2 ([ScriptBlock]$Expression, [int]$Samples = 1, [Switch]$Silent) {
    function Measure-Command2 ([ScriptBlock]$Expression, [int]$Samples = 1, [Switch]$Silent, [Switch]$Long) {
    <#
    .SYNOPSIS
    Runs the given script block and returns the execution duration.
    @@ -31,9 +31,18 @@ function Measure-Command2 ([ScriptBlock]$Expression, [int]$Samples = 1, [Switch]

    $stats = $timings | Measure-Object -Average -Minimum -Maximum -Property Ticks

    Write-Host "Avg: $((New-Object System.TimeSpan $stats.Average).ToString())"
    Write-Host "Min: $((New-Object System.TimeSpan $stats.Minimum).ToString())"
    Write-Host "Max: $((New-Object System.TimeSpan $stats.Maximum).ToString())"
    # Print the full timespan if the $Long switch was given.
    if ($Long) {
    Write-Host "Avg: $((New-Object System.TimeSpan $stats.Average).ToString())"
    Write-Host "Min: $((New-Object System.TimeSpan $stats.Minimum).ToString())"
    Write-Host "Max: $((New-Object System.TimeSpan $stats.Maximum).ToString())"
    }
    else {
    # Otherwise just print the milliseconds which is easier to read.
    Write-Host "Avg: $((New-Object System.TimeSpan $stats.Average).TotalMilliseconds)ms"
    Write-Host "Min: $((New-Object System.TimeSpan $stats.Minimum).TotalMilliseconds)ms"
    Write-Host "Max: $((New-Object System.TimeSpan $stats.Maximum).TotalMilliseconds)ms"
    }
    }

    Set-Alias time Measure-Command2
  2. jpoehls revised this gist Mar 26, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    PS> time { ping -n 1 google.com } -Samples 10 -Silent
    ..........
    Avg: 00:00:00.0705483
    Min: 00:00:00.0648462
    Max: 00:00:00.1081890
  3. jpoehls revised this gist Mar 26, 2012. 1 changed file with 32 additions and 7 deletions.
    39 changes: 32 additions & 7 deletions time.ps1
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,39 @@
    function Measure-ExecutionTime ($cmd) {
    function Measure-Command2 ([ScriptBlock]$Expression, [int]$Samples = 1, [Switch]$Silent) {
    <#
    .SYNOPSIS
    Runs the given script block and returns the execution duration.
    Discovered on StackOverflow. http://stackoverflow.com/questions/3513650/timing-a-commands-execution-in-powershell
    .EXAMPLE
    Measure-ExecutionTime { ping -n 1 google.com }
    Measure-Command2 { ping -n 1 google.com }
    #>
    $sw = [Diagnostics.Stopwatch]::StartNew()
    & $cmd
    $sw.Stop()
    $sw.Elapsed.ToString()
    }
    $timings = @()
    do {
    $sw = New-Object Diagnostics.Stopwatch
    if ($Silent) {
    $sw.Start()
    $null = & $Expression
    $sw.Stop()
    Write-Host "." -NoNewLine
    }
    else {
    $sw.Start()
    & $Expression
    $sw.Stop()
    }
    $timings += $sw.Elapsed

    $Samples--
    }
    while ($Samples -gt 0)

    Write-Host

    $stats = $timings | Measure-Object -Average -Minimum -Maximum -Property Ticks

    Write-Host "Avg: $((New-Object System.TimeSpan $stats.Average).ToString())"
    Write-Host "Min: $((New-Object System.TimeSpan $stats.Minimum).ToString())"
    Write-Host "Max: $((New-Object System.TimeSpan $stats.Maximum).ToString())"
    }

    Set-Alias time Measure-Command2
  4. jpoehls created this gist Mar 26, 2012.
    14 changes: 14 additions & 0 deletions time.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    function Measure-ExecutionTime ($cmd) {
    <#
    .SYNOPSIS
    Runs the given script block and returns the execution duration.
    Discovered on StackOverflow. http://stackoverflow.com/questions/3513650/timing-a-commands-execution-in-powershell
    .EXAMPLE
    Measure-ExecutionTime { ping -n 1 google.com }
    #>
    $sw = [Diagnostics.Stopwatch]::StartNew()
    & $cmd
    $sw.Stop()
    $sw.Elapsed.ToString()
    }