Equivalent of Unix time command in PowerShell?

powershell

Solution

I used both the times provided by `System.Diagnostics.Process` and `GetProcessTimes` to come up with an implementation for `time`:

$source=@'

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class Timer
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool GetProcessTimes(IntPtr handle, out long creation, out long exit, out long kernel,
                                                  out long user);

        public static void Time(string file,string args)
        {
            long user,kernel,exit,creation;
            Process proc = null;
            proc = Process.Start(file,args);
            proc.WaitForExit();
            GetProcessTimes(proc.Handle, out creation, out exit, out kernel, out user);
            long real = exit - creation;
            Console.WriteLine("real {0}\nuser {1}\nsys {2}", real / 10000000.0, user/10000000.0,kernel/10000000.0);
        }
    }
'@

Add-Type -TypeDefinition $source -Language CSharpVersion3

function time ($scriptblock) {

    $file = "powershell";
    $args = $scriptblock;

    $startInfo = new-object Diagnostics.ProcessStartInfo;
    $startInfo.FileName = $file;
    $startInfo.Arguments = $args;
    $startInfo.CreateNoWindow = $true;
    $startInfo.UseShellExecute = $false;
    $startInfo.RedirectStandardOutput = $true;
    $process = [Diagnostics.Process]::Start($startInfo);
    $process.WaitForExit();
    write-host $process.StandardOutput.ReadToEnd();
    write-host real: ($process.ExitTime - $process.StartTime)
    write-host user: $process.UserProcessorTime;
    write-host sys:  $process.PrivilegedProcessorTime;
    write-host using GetProcessTimes
    [Timer]::Time($file,$args)
}

time {sleep 10}

It isn't really perfect as the real time comes out as about 11 seconds ( for `sleep 10` ) since I am creating a powershell process and running the command in that. I will see if I can implement a cmdlet or something for this.

Problem

Earlier I was reading a great answer on SO and I wondered why it has not been emulated yet in PowerShell. In unix/linux, we can use the `time` command as a simple benchmark tool. ``` $ time ./script1.sh real 0m1.005s user 0m0.000s sys 0m0.008s ``` In powershell, we can use the `measure-command` similarly: ``` $ Measure-Command {java post_incr} Days : 0 Hours : 0 Minutes : 0 Seconds : 1 Milliseconds : 18 Ticks : 10188003 TotalDays : 1.17916701388889E-05 TotalHours : 0.000283000083333333 TotalMinutes : 0.016980005 TotalSeconds : 1.0188003 TotalMilliseconds : 1018.8003 ``` But this is not the same as the `time`, which reports the real, user and sys (See the difference between the three in the linked SO Answer.) This (`time`) is obviously such a useful little tool. Are there any user written cmdlets for this functionality or is it already in V3 or planned for future releases?

Original source

Related problems