Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Unix time command in PowerShell?

Tags:

powershell

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?

like image 320
Animesh Avatar asked Nov 11 '11 06:11

Animesh


1 Answers

Warning, PowerShell ahead.

Some coffee helped me come up with this:

function time { $Command = "$args"; Measure-Command { Invoke-Expression $Command 2>&1 | out-default} }

And if you want it to output nothing, just replace with out-null:

function timequiet { $Command = "$args"; Measure-Command { Invoke-Expression $Command 2>&1 | out-null} }

You use it like this:

PS C:\> time sleep 5


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 4
Milliseconds      : 990
Ticks             : 49906722
TotalDays         : 5,77624097222222E-05
TotalHours        : 0,00138629783333333
TotalMinutes      : 0,08317787
TotalSeconds      : 4,9906722
TotalMilliseconds : 4990,6722



PS C:\>
like image 70
Andrei Ghimus Avatar answered Oct 04 '22 10:10

Andrei Ghimus