相当于“时间”的Windows命令行在Linux中? [英] Windows Command Line Equivalent to "time" in Linux?

查看:52
本文介绍了相当于“时间”的Windows命令行在Linux中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个愚蠢的问题,但似乎无法在网上找到答案。在基于linux的系统中,在终端中,在任何命令给出实际时间,用户时间和系统时间之前,在终端中键入 time。例如,键入

I've got what might be a dumb question but I can't seem to find the answer anywhere online. In linux based systems, in the terminal typing "time" before any command gives how long the command takes in terms of real, user, and system time. For example, typing

time ls

列出当前目录中的文件和文件夹,然后给出列出文件和文件夹所花费的实际时间,用户时间和系统时间。是否有与之等效的窗户?我正在尝试比较不同算法的性能,但是没有可运行的linux计算机,所以我希望Windows中有类似的命令。

lists the files and folders in the current directory then gives the amount of real, user, and system time that it took to list the files and folders. Is there a windows equivalent to this? I am trying to compare the performance of different algorithms but don't have a linux machine to work on so I was hoping that there was a similar command in Windows.

推荐答案

以下内容远非完美。但是,它是模拟UNIX time 行为的最接近的方法。我确信它可以改进很多。

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.

基本上,我正在创建一个cmdlet,该cmdlet可以接收脚本块,生成进程并使用 GetProcessTimes 获取内核时间,用户时间和经过时间。

Basically I'm creating a cmdlet that receives a script block, generates a process and uses GetProcessTimes to get Kernel, User and Elapsed times.

一旦加载了cmdlet,只需用

Once the cmdlet is loaded, just invoke it with

测量时间-命令{您的命令} [-静默]

-Silent 开关表示该命令未生成任何输出(即,您仅对时间度量感兴趣)

The -Silent switch means no output generated from the command (I.e you are interested only in the time measures)

因此例如:

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

这篇关于相当于“时间”的Windows命令行在Linux中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆