Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Command Line Equivalent to "time" in Linux? [duplicate]

I've got what might be a dumb question but I can't seem to find the answer anywhere online. In linux based systems, in the terminal typing "time" before any command gives how long the command takes in terms of real, user, and system time. For example, typing

time ls

lists the files and folders in the current directory then gives the amount of real, user, and system time that it took to list the files and folders. Is there a windows equivalent to this? I am trying to compare the performance of different algorithms but don't have a linux machine to work on so I was hoping that there was a similar command in Windows.

like image 643
Code Avatar asked Dec 03 '14 02:12

Code


People also ask

What is the time command on Windows?

In computing, TIME is a command in DEC RT-11, DOS, IBM OS/2, Microsoft Windows and a number of other operating systems that is used to display and set the current system time. It is included in command-line interpreters (shells) such as COMMAND.COM , cmd.exe , 4DOS, 4OS2 and 4NT.

How do I measure execution time of a command on the Windows command line?

Example: cmd /v:on /c echo ! TIME! & echo "Quoted mycommand" & cmd /v:on /c echo ! TIME! . Simpler: echo %time% & *mycommand* & call echo %^time% .

What is the Windows equivalent of wc?

The Linux/Unix "line count" command, wc -l , has a Windows equivalent find /c /v "" .

What time is it Linux command?

check time with timedatectl command in Linux To use the timedatectl command, type “timedatectl” at the command prompt. This will display the current system time and date settings.


2 Answers

I found a similar question on SuperUser which covers some alternatives. First and foremost being my suggestion to use Measure-Command in PowerShell.

Measure-Command {ls}

Got the syntax wrong in my comment.

like image 117
Matt Avatar answered Oct 10 '22 17:10

Matt


The following is far from perfect. But it's the closest I could come up with to simulate UNIX time behavior. I'm sure it can be improved a lot.

Basically I'm creating a cmdlet that receives a script block, generates a process and uses GetProcessTimes to get Kernel, User and Elapsed times.

Once the cmdlet is loaded, just invoke it with

Measure-Time -Command {your-command} [-silent]

The -Silent switch means no output generated from the command (I.e you are interested only in the time measures)

So for example:

Measure-Time -Command {Get-Process;sleep -Seconds 5} -Silent

The output generated:

Kernel time : 0.6084039
User time   : 0.6864044
Elapsed     : 00:00:06.6144000

Here is the cmdlet:

Add-Type -TypeDefinition @" 
using System; 
using System.Runtime.InteropServices;

public class ProcessTime 
{ 
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
    public static extern bool GetProcessTimes(IntPtr handle, 
                                              out IntPtr creation, 
                                              out IntPtr exit, 
                                              out IntPtr kernel,
                                              out IntPtr user);
}
"@

function Measure-Time
{
    [CmdletBinding()]
    param ([scriptblock] $Command,
    [switch] $Silent = $false
    )

    begin
    {
        $creation = 0
        $exit = 0
        $kernel = 0
        $user = 0
        $psi = new-object diagnostics.ProcessStartInfo
        $psi.CreateNoWindow = $true
        $psi.RedirectStandardOutput = $true
        $psi.FileName = "powershell.exe"
        $psi.Arguments = "-command $Command"
        $psi.UseShellExecute = $false
    }
    process
    {
        $proc = [diagnostics.process]::start($psi)
        $buffer = $proc.StandardOutput.ReadToEnd()    

        if (!$Silent)
        {
            Write-Output $buffer
        }
        $proc.WaitForExit()
    }

    end
    {
        $ret = [ProcessTime]::GetProcessTimes($proc.handle,
                                      [ref]$creation,
                                      [ref]$exit,
                                      [ref]$kernel,
                                      [ref]$user
                                      )
        $kernelTime = [long]$kernel/10000000.0
        $userTime = [long]$user/10000000.0
        $elapsed = [datetime]::FromFileTime($exit) - [datetime]::FromFileTime($creation)

        Write-Output "Kernel time : $kernelTime"
        Write-Output "User time   : $userTime"
        Write-Output "Elapsed     : $elapsed"
    }
}
like image 33
Micky Balladelli Avatar answered Oct 10 '22 17:10

Micky Balladelli