微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Windows命令行相当于Linux中的“时间”?

参见英文答案 > How do I measure execution time of a command on the Windows command line?29个
> Equivalent of Unix time command in PowerShell?4个
我有一个可能是一个愚蠢的问题,但我似乎无法在网上找到答案.在基于linux的系统中,在终端中输入“time”之前,任何命令都会根据实际,用户和系统时间给出命令所需的时间.例如,输入
time ls

列出当前目录中的文件文件夹,然后列出列出文件文件夹所花费的实际,用户和系统时间.有窗户相当吗?我试图比较不同算法的性能,但没有Linux机器工作,所以我希望在Windows中有类似的命令.

以下内容远非完美.但它是我能够最接近模拟UNIX时间行为的.我相信它可以改进很多.

基本上我正在创建一个接收脚本块的cmdlet,生成一个进程并使用GetProcesstimes来获取Kernel,User和Elapsed次.

加载cmdlet后,只需调用它即可

Measure-Time -Command {your-command} [-silent]

-Silent开关意味着没有从命令生成输出(即,您只对时间测量感兴趣)

例如:

Measure-Time -Command {Get-Process;sleep -Seconds 5} -Silent

生成输出

Kernel time : 0.6084039
User time   : 0.6864044
Elapsed     : 00:00:06.6144000

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

原文地址:https://www.jb51.cc/windows/363585.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐