10

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.

Code
  • 141
  • 1
  • 8
  • I looked over that answer but was curious as to whether there was a way to get separate readings of the system and user time as opposed to just real time. – Code Dec 03 '14 at 02:51
  • In PowerShell you can pipe into `Measure-Command` but i dont know of the different time breakdowns. `ls | Measure-Command` – Matt Dec 03 '14 at 02:59
  • In Windows, there is rarely a reason to distinguish between user time and kernel time. You would probably need to write your own code if you want to do that. – Harry Johnston Dec 03 '14 at 03:29

2 Answers2

7

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"
    }
}
Micky Balladelli
  • 9,053
  • 2
  • 27
  • 27
5

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.

Community
  • 1
  • 1
Matt
  • 40,384
  • 7
  • 62
  • 97
  • I just tried that out. It looks like that only gives the real time though. Do you know of a way to get the system time or user time? – Code Dec 03 '14 at 03:15
  • Mentioned in the comments, not really sure what that means. If you look at the post linked there isnt a clear consensus there either – Matt Dec 03 '14 at 03:30
  • I don't know exactly what system time and user time represent but I know that they give the time that a command takes to run independent of the hardware. My guess is that they count clock cycles rather than seconds. – Code Dec 03 '14 at 03:42