比较对象PowerShell性能和操作VS循环

时间:2013-05-22 04:44:31

标签: windows performance powershell

我正在查看this question,其中OP想要知道如何比较两个数组中的项目而不循环遍历每个数组。

给出的命令是:

$array3 = @(Compare-Object $array1 $array2 | select -Expand InputObject

我的问题是双重的:

一,这是否真的避免以任何形式迭代数组?或者它只是通过在幕后进行操作来简单地模糊用户的操作。

两,就性能而言,这是比较对象的最佳方法吗?在我看来它实际上要慢得多。

我做了一个真正的原油测试:

$Array1 = @("1","2","Orchid","Envy","Sam","Map Of the World","Short String","s","V","DM","qwerty","1234567891011")
$Array2 = @("Bob", "Helmet", "Jane")

$Date1 = Get-Date
$Array2 | ForEach-Object `
    {
    if ($Array1 -contains $_){}

    }
$Date2 = Get-Date
$Time1 = [TimeSpan]$Date2.Subtract($Date1)
Write-Host $Time1

$Date1 = Get-Date
$Array3 = @(Compare-Object $Array1 $Array2)
$Date2 = Get-Date
$Time2 = [TimeSpan]$Date2.Subtract($Date1)
Write-Host $Time2

我的时代出现了:

ForEach-Object:00:00:00.0030001

Compare-Object:00:00:00.0030002


修改

我更新了脚本以使其更公平,并且它基本上平息了时间。

那么Compare-Object和传统循环之间的幕后差异是什么?假设没有,我是否正确?


编辑2

我使用反编译器找到了这段代码:

internal int Compare(ObjectCommandPropertyValue first, ObjectCommandPropertyValue second)
    {
      if (first.IsExistingProperty && second.IsExistingProperty)
        return this.Compare(first.PropertyValue, second.PropertyValue);
      if (first.IsExistingProperty)
        return -1;
      return second.IsExistingProperty ? 1 : 0;
    }

    public int Compare(object first, object second)
    {
      if (ObjectCommandComparer.IsValueNull(first) && ObjectCommandComparer.IsValueNull(second))
        return 0;
      PSObject psObject1 = first as PSObject;
      if (psObject1 != null)
        first = psObject1.BaseObject;
      PSObject psObject2 = second as PSObject;
      if (psObject2 != null)
        second = psObject2.BaseObject;
      try
      {
        return LanguagePrimitives.Compare(first, second, !this.caseSensitive, (IFormatProvider) this.cultureInfo) * (this.ascendingOrder ? 1 : -1);
      }
      catch (InvalidCastException ex)
      {
      }
      catch (ArgumentException ex)
      {
      }
      return string.Compare(((object) PSObject.AsPSObject(first)).ToString(), ((object) PSObject.AsPSObject(second)).ToString(), !this.caseSensitive, this.cultureInfo) * (this.ascendingOrder ? 1 : -1);
    }

我尽可能地追踪它,我相信这些是两个工作线程。看起来比较对象实际上只做1< ==> 1查看列表。我在这里错过了什么吗?

0 个答案:

没有答案