Cod*_*ode 6 linux windows powershell command-line
我有一个可能是一个愚蠢的问题,但我似乎无法在网上找到答案.在基于linux的系统中,在终端中输入"time"之前,任何命令都会根据实际,用户和系统时间给出命令所需的时间.例如,输入
time ls
Run Code Online (Sandbox Code Playgroud)
列出当前目录中的文件和文件夹,然后列出列出文件和文件夹所花费的实际,用户和系统时间.有窗户相当吗?我试图比较不同算法的性能,但没有Linux机器工作,所以我希望在Windows中有类似的命令.
以下内容远非完美.但它是我能够最接近模拟UNIX time行为的.我相信它可以改进很多.
基本上我正在创建一个接收脚本块的cmdlet,生成一个进程并用于GetProcessTimes获取内核,用户和经过的时间.
加载cmdlet后,只需调用它即可
Measure-Time -Command {your-command} [-silent]
该-Silent开关装置不从命令生成的输出(即要感兴趣的只是时间的措施)
例如:
Measure-Time -Command {Get-Process;sleep -Seconds 5} -Silent
Run Code Online (Sandbox Code Playgroud)
生成的输出:
Kernel time : 0.6084039
User time : 0.6864044
Elapsed : 00:00:06.6144000
Run Code Online (Sandbox Code Playgroud)
这是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"
}
}
Run Code Online (Sandbox Code Playgroud)
我在SuperUser上发现了一个类似的问题,其中涵盖了一些替代方案。首先也是最重要的是我建议Measure-Command在 PowerShell 中使用。
Measure-Command {ls}
Run Code Online (Sandbox Code Playgroud)
在我的评论中语法错误。
| 归档时间: |
|
| 查看次数: |
3687 次 |
| 最近记录: |