Windows Command Line Equivalent to "Time" in Linux

Windows Command Line Equivalent to time in Linux?

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"
}
}

what is the windows equivalent of the command date+%s

Here is a native batch solution that should work in any locale. It uses WMIC to get the current local time in a locale independent manner. Everything else is a "simple" matter of string parsing and basic math.

:UnixTime  [ReturnVar]  [TimeStamp]
::
:: Computes the Unix time from the current local time as reported by the
:: operating system. The Unix time is the number of seconds that have elapsed
:: since midnight Coordinated Universal Time (UTC), January 1, 1970, not
:: counting leap seconds.
::
:: The result is returned in variable ReturnVar,
:: or the result is echoed if ReturnVar is not specified
::
:: If the TimeStamp is provided in the 2nd parameter, then the Unix time for
:: the TimeStamp is computed, rather then for the current time.
::
:: The TimeStamp must have the same format as used by WMIC:
::
:: YYYYMMDDhhmmss.ffffffSzzz
::
:: where:
::
:: YYYY = gregorian year
:: MM = month
:: DD = day
:: hh = hour in 24 hour format
:: mm = minute
:: ss = seconds
:: ffffff = fractional seconds (microseconds)
:: S = timezone sign: + or -
:: zzz = timezone: minutes difference from GMT
::
:: Each component must be zero prefixed as needed to maintain the proper width.
::
:: The ReturnVar parameter must be provided in order to use the TimeStamp.
:: A ReturnVar of "" will function the same as no ReturnVar. This enables the
:: specification of a TimeStamp without an actual ReturnVar.
::
@echo off
setlocal
set "ts=%~2"
if not defined ts for /f "skip=1 delims=" %%A in ('wmic os get localdatetime') do if not defined ts set "ts=%%A"
set /a "yy=10000%ts:~0,4% %% 10000, mm=100%ts:~4,2% %% 100, dd=100%ts:~6,2% %% 100"
set /a "dd=dd-2472663+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4"
set /a ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100-%ts:~21,1%((1%ts:~22,3%*60)-60000)
set /a ss+=dd*86400
endlocal & if "%~1" neq "" (set %~1=%ss%) else echo %ss%
exit /b



Note that this solution has a limited life span. It will cease to work on 2038-01-19 when the Unix time exceeds the maximum value of a signed 32 bit integer.

EDIT - The code has been edited to support conversion of a timestamp string on the command line instead of the current local time. The precise range of times supported is 1901-12-13 20:45:52.000000 through 2038-01-19 03:14:07.999999 GMT. Times prior to 1970-01-01 00:00:00.000000 will yield negative values.

How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?

See Windows Batch File (.bat) to get current date in MMDDYYYY format:

@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime%

If you prefer the time in 24 hour/military format, you can replace the second FOR line with this:

For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

C:> .\date.bat

2008-10-14_0642

If you want the date independently of the region day/month order, you can use "WMIC os GET LocalDateTime" as a source, since it's in ISO order:

@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo Local date is [%ldt%]

C:>test.cmd

Local date is [2012-06-19 10:23:47.048]

Equivalent of Unix time command in PowerShell?

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.

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

If you are using Windows 2003 (note that windows server 2008 and later are not supported) you can use The Windows Server 2003 Resource Kit, which contains timeit.exe that displays detailed execution stats. Here is an example, timing the command "timeit -?":

C:\>timeit timeit -?
Invalid switch -?
Usage: TIMEIT [-f filename] [-a] [-c] [-i] [-d] [-s] [-t] [-k keyname | -r keyname] [-m mask] [commandline...]
where: -f specifies the name of the database file where TIMEIT
keeps a history of previous timings. Default is .\timeit.dat
-k specifies the keyname to use for this timing run
-r specifies the keyname to remove from the database. If
keyname is followed by a comma and a number then it will
remove the slowest (positive number) or fastest (negative)
times for that keyname.
-a specifies that timeit should display average of all timings
for the specified key.
-i specifies to ignore non-zero return codes from program
-d specifies to show detail for average
-s specifies to suppress system wide counters
-t specifies to tabular output
-c specifies to force a resort of the data base
-m specifies the processor affinity mask

Version Number: Windows NT 5.2 (Build 3790)
Exit Time: 7:38 am, Wednesday, April 15 2009
Elapsed Time: 0:00:00.000
Process Time: 0:00:00.015
System Calls: 731
Context Switches: 299
Page Faults: 515
Bytes Read: 0
Bytes Written: 0
Bytes Other: 298

You can get TimeIt in the Windows 2003 Resource Kit. It's not available for direct download from the Microsoft Download Center, but one can still get it from the archive.org - Windows Server 2003 Resource Kit Tools.

Windows equivalent of the Linux command 'touch'?

If you want to touch the date stamp of a file using windows, use the following command at the command prompt:

copy /b filename.ext +,,

(where filename.ext is your file's name). The +,, is a special flag to copy telling it to simply update the date/time on the file:

* Changing the time and date of a file

If you want to assign the current time and date to a file without modifying the file, use the following syntax:

copy /b Source+,,

The commas indicate the omission of the Destination parameter.

Edit based on comments by Lumi and Justin: put this in a batch file, eg. touch.cmd

@COPY /B %1+,, %1

This works even if the file is not in the current directory (tested on Windows 7).

How do I time a program executing in Windows?

timeit from the Windows Server 2003 Resource Kit should do the trick.



Related Topics



Leave a reply



Submit