Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time command equivalent in PowerShell

What is the flow of execution of the time command in detail?

I have a user created function in PowerShell, which will compute the time for execution of the command in the following way.

  1. It will open the new PowerShell window.
  2. It will execute the command.
  3. It will close the PowerShell window.
  4. It will get the the different execution times using the GetProcessTimes function function.

Is the "time command" in Unix also calculated in the same way?

like image 472
Raj Avatar asked Nov 14 '13 11:11

Raj


People also ask

How to Measure time in PowerShell?

Using the Measure-Command cmdlet in PowerShell is an easy way to measure the run-time or execution time of a command. This can also tell you how long a custom function or an entire script takes to execute. Measure-Commandis a cmdlet that is easy to use and provides intuitive, actionable output.

What does $() mean in PowerShell?

$() is a special operator in PowerShell commonly known as a subexpression operator. It is used when we have to use one expression within some other expression. For instance, embedding the output of a command with some other expression.

What does $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


2 Answers

The Measure-Command cmdlet is your friend.

PS> Measure-Command -Expression {dir}

You could also get execution time from the command history (last executed command in this example):

$h = Get-History -Count 1
$h.EndExecutionTime - $h.StartExecutionTime
like image 157
Shay Levy Avatar answered Sep 23 '22 04:09

Shay Levy


I've been doing this:

Time {npm --version ; node --version}

With this function, which you can put in your $profile file:

function Time([scriptblock]$scriptblock, $name)
{
    <#
        .SYNOPSIS
            Run the given scriptblock, and say how long it took at the end.

        .DESCRIPTION

        .PARAMETER scriptBlock
            A single computer name or an array of computer names. You mayalso provide IP addresses.

        .PARAMETER name
            Use this for long scriptBlocks to avoid quoting the entire script block in the final output line

        .EXAMPLE
            time { ls -recurse}

        .EXAMPLE
            time { ls -recurse} "All the things"
    #>

    if (!$stopWatch)
    {
        $script:stopWatch = new-object System.Diagnostics.StopWatch
    }
    $stopWatch.Reset()
    $stopWatch.Start()
    . $scriptblock
    $stopWatch.Stop()
    if ($name -eq $null) {
        $name = "$scriptblock"
    }
    "Execution time: $($stopWatch.ElapsedMilliseconds) ms for $name"
}

Measure-Command works, but it swallows the stdout of the command being run. (Also see Timing a command's execution in PowerShell)

like image 39
Eris Avatar answered Sep 26 '22 04:09

Eris