【问题标题】:Measure execution time in minutes and seconds以分钟和秒为单位测量执行时间
【发布时间】:2017-05-30 08:55:54
【问题描述】:

我想在自动化 PowerShell 脚本中进行多次测量。我使用了 Get-Date 和 TotalSeconds。结果看起来像这样:236.908

如何以分钟和秒为单位获得人类可读的经过时间?

$startTime = (Get-Date)
$endTime = (Get-Date)

$ElapsedTime = (($endTime-$startTime).TotalSeconds)

Write-Host "Duration: xx min xx sec"

【问题讨论】:

  • ($endTime-$startTime).ToString('''Duration: ''mm'' min ''ss'' sec''')
  • @PetSerAl 嗨,为什么引用狂热?
  • 有效!谢谢。

标签: powershell


【解决方案1】:

使用format operator (-f):

'Duration: {0:mm} min {0:ss} sec' -f ($endTime-$startTime)

或者像这样,如果您也需要其他地方的差异:

$ElapsedTime = $endTime-$startTime
'Duration: {0:mm} min {0:ss} sec' -f $ElapsedTime

【讨论】:

    【解决方案2】:

    你也可以使用Measure-Command

    Measure-Command -Expression {
    
        # Command 1
        Get-ChildItem
    
        # Command N
        Get-Process
    }
    

    或者像这样:

    $result = Measure-Command -Expression {
    
        # Command 1
        Get-ChildItem
    
        # Command N
        Get-Process
    }
    
    $result.ToString()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 2014-07-11
      • 2016-12-14
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多