Windows命令行中类似Linux的"time"命令的等效命令是什么?

11

我有一个可能很蠢的问题,但我似乎无法在网上找到答案。在基于Linux的系统中,在终端输入任何命令之前键入“time”可以显示该命令所需时间的实际时间、用户时间和系统时间。例如,输入

time ls

列出当前目录中的文件和文件夹,然后给出列出这些文件和文件夹所花费的实际时间、用户时间和系统时间的数量。是否有 Windows 的等效命令?我想比较不同算法的性能,但没有 Linux 机器可以使用,因此希望 Windows 中有类似的命令。


我看了那个答案,但是我很好奇是否有一种方法可以获取系统时间和用户时间的单独读数,而不仅仅是实际时间。 - Code
在PowerShell中,您可以将内容管道传递到“Measure-Command”,但我不知道不同的时间分解。ls | Measure-Command - Matt
在Windows中,很少有理由区分用户时间和内核时间。如果您想这样做,可能需要编写自己的代码。 - Harry Johnston
2个回答

8
以下内容远非完美,但是这是我能够想到的最接近模拟UNIX时间行为的方法。我相信它可以得到很大改进。
基本上,我正在创建一个cmdlet,该cmdlet接收脚本块,生成一个进程并使用GetProcessTimes来获取内核、用户和经过时间。
一旦加载了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"
    }
}

7
我在SuperUser上找到了一个类似的问题,其中涵盖了一些替代方案。首先是我建议在PowerShell中使用Measure-Command的方法。
Measure-Command {ls}

我的评论中语法错误了。


(意思是说:在我的评论中,语法有误。)

我刚试过了。看起来那只给出实时时间。你知道如何获取系统时间或用户时间吗? - Code
在评论中提到,我不太确定那是什么意思。如果你看一下链接的帖子,那里也没有明确的共识。 - Matt
我不确定系统时间和用户时间具体代表什么,但我知道它们提供了一个命令运行所需的时间,与硬件无关。我的猜测是它们计算的是时钟周期而不是秒数。 - Code

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接