gpt4 book ai didi

arrays - 将数组传递给函数非常慢

转载 作者:行者123 更新时间:2023-12-03 14:55:31 25 4
gpt4 key购买 nike

我编写了一个 PowerShell 脚本,需要在函数之间传递大字节数组。脚本运行非常慢慢地。下面的示例代码重现了该问题。它只是将一个 10 MiB 的数组传递给一个什么都不做(除了记录一条消息)的函数,并测量调用需要多长时间。

function do-nothing
{
param($foo)
Write-Verbose -Verbose "in the function"
}

$tenMiB = 10 * 1024 * 1024
$myFoo = New-Object byte[] $tenMiB

Write-Verbose -Verbose "about to do call a function that does nothing..."
$elapsed = Measure-Command -Expression { do-nothing -foo $myFoo }
Write-Verbose -Verbose "doing nothing took $($elapsed.TotalMilliseconds) milliseconds"

我希望这需要几毫秒。在我的家用 PC 上,它需要将近 3000 毫秒并以 100% 的速度运行 CPU 内核。我使用 PowerShell 4 和 PowerShell 5(预生产版本)得到了相同的结果。我的工作 PC 和服务器也会发生同样的事情。然而,一位同事得到了预期的结果——几毫秒的执行时间,最小的 CPU 使用率。

执行时间大致与数组的大小成正比,这对我来说毫无意义。数组的大小应该无关紧要。

我想知道数组是否以某种方式被复制。我不认为这是因为调用 GetHashCode()在数组上返回函数内部和外部相同的值。

如果数组被包裹在 PSCustomObject ,并将包装器传递给函数,然后代码按预期执行。作为一种解决方法,这有点难看,我宁愿在使用它之前了解发生了什么。

谁能解释一下这种看似奇怪的行为吗?

最佳答案

我将其作为社区 wiki 发布,旨在成为我们可以发布个人环境和测试结果的地方,因为复制似乎有些麻烦。也许我们可以找出导致这种情况的条件,并从那里确定为什么它在这种情况下很慢。欢迎格式化建议/更改。

briantist

工作站

  • HW (i7)
  • Windows 8.1
  • PowerShell 4.0
  • ISE and Console
  • Domain joined
  • byte[] Result: ~3,500ms
  • object[] Result: ~6,500ms


服务器虚拟机

  • VM (VMware ESXi)
  • Windows 2012 R2
  • PowerShell 5.0.10514.6 (Production Preview)
  • ISE
  • Domain joined
  • byte[] Result: > 7,000ms
  • object[] Result: ~100ms


家庭工作站

  • HW (i5)
  • Windows 10 build 1511
  • PowerShell 5
  • ISE and Console
  • Workgroup (No AD)
  • Result: 4-6ms first run, < 2ms subsequent runs


emanresu

  • PowerShell 4
  • Result: 1.8 - 2.4ms


Bacon Bits

工作站

  • Windows 7 x64 SP1
  • Domain joined
  • PowerShell 4
  • Result: 100 executions, average of 1.16ms, min 0.67ms, max 13.88ms, 1.39ms stddev


虚拟机 (ESXi)

  • Windows Server 2012 R2
  • Domain joined
  • PowerShell 4
  • Result: 10 executions, average of 4221ms, min 4173ms, max 4302ms, 38.54ms stddev


Ansgar Wiechers

虚拟机(虚拟机)

  • Windows 2012 R2 (vanilla install)
    • PowerShell 4
      CLRVersion: 4.0.30319.34014
      BuildVersion: 6.3.9600.16394
    • Result: ~7.5ms
  • Windows 2012 R2 (same VM, fully patched)
    • PowerShell 4
      CLRVersion: 4.0.30319.34209
      BuildVersion: 6.3.9600.17400
    • Result: ~4000ms


进一步调查显示该问题是由 2014 年 11 月的更新汇总 ( MSKB 3000850) 引入的。不过,不确定哪些收集到的修补程序是真正的罪魁祸首。

beatcracker

桌面

  • HW: Q6600@3.1 GHz/8GB RAM
  • Windows Server 2012
  • Workgroup (No AD)
  • PowerShell 5 (5.0.10018.0)
  • ISE and Console
  • Result: ~5000ms


笔记本电脑

  • HW: i3@2.5 GHz/4GB RAM
  • Windows 8.1
  • Domain-joined PC
  • PowerShell 4
  • ISE and Console
  • Result: ~6000ms


David Brabant

  • PowerShell 5
  • Windows 10
  • Result: < 7 ms


sodawillow

  • HW (i5)
  • Windows 8.1
  • Workgroup (No AD)
  • PowerShell 5.0.10514.6
  • ISE
  • Result: > 3500 ms


Mathias R. Jessen

家用笔记本电脑

  • HW (i7-2620M)
  • Windows 10 (build 10240)
  • PowerShell 5.0
  • ISE and Console
  • byte[] Result: ~5,350ms
  • object[] Result: ~8,500ms
  • int[] Result: ~9,700ms

关于arrays - 将数组传递给函数非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com