最好的类型,用于存储多维数据以提高速度

时间:2018-12-03 05:57:39

标签: powershell

我有一个脚本,用于比较数据库链接并运行测试路径以检查其是否存在,将不存在的所有内容添加到自定义对象中。对于较旧的客户端,从文件系统而不是数据库中删除的文档数量非常庞大。所以我想加快一点:

Measure-Command -Expression {
  $count = 10000
  $hashtablelistofitems = @{}
  for ($i=0; $i -le $count; $i++) 
  {
    $hashtablelistofitems.add("$i", "i'm a value")
  }
} | Select-Object -Property TotalMilliSeconds



Measure-Command -Expression {
  $count = 10000

  $array = @()
  $listofitems = "" | select key,value
  for ($x=0; $x -le $count; $x++) 
  {
    $listofitems.key = "$x"
    $listofitems.value = "i'm a value"
    $array += $listofitems
  }
} | Select-Object -Property TotalMilliSeconds



Measure-Command -Expression {
  $count = 10000

  $myitems = @()
  for ($x=0; $x -le $count; $x++) 
  {
    $myitems += @([pscustomobject]@{key=$x.path;ID="i'm a value"})
  }
} | Select-Object -Property TotalMilliSeconds

目前我正在使用第三种方法,这些命令的结果是:

TotalMilliseconds
-----------------
          40.0566
        2609.2074
        3061.0848

因此,正如您所看到的,我使用的是最慢的方法,哈希表比自定义对象快得多。我的问题是,我在将哈希表值以简洁的格式传递到报告模块(例如Export-Excel)时遇到问题,无法发送给客户端。

我还能使用其他多维数据类型吗?还是有办法提高我当前使用的自定义对象的速度?

这是我用来获取数据的代码:

    foreach ($file in $files) {
        if (!(Test-path $file.Path)) {
            $myitems += 
                @([pscustomobject]@{path=$file.path;ID=$file.ID;ObjectID=$file."Object ID";ObjectType=$file."Object Type"})
        }
        Write-Verbose "Processed :: $($file.path)"
    } 

然后仅将$myitems传送到export-excel

2 个答案:

答案 0 :(得分:2)

第三种方法之所以缓慢的原因是数组大小调整是一项昂贵的操作。在.Net中(因此在Powershell中),数组具有静态大小。添加更多元素时,将创建一个更大尺寸的新数组,并将所有元素复制到其中。这使得很多额外的工作循环进行。

将数组初始化为适当的大小可使循环变得轻松。像这样

 <h2>Math Test</h2>
 <div id="mathDiv">1. Solve for x $$X^2-1 = 8$$. 
     <br>2. Evaluate the following limit: $$\lim_{x \rightarrow b}{  (x-10)  }$$
     <br>3. Is ${  x  } ^ {  4  } = 81 $ if $ x^4 - 9 =0 $ ?
 </div>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
 <script type="text/x-mathjax-config">
   MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
   src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<script>
   $(document).ready(function() {
   MathJax.Hub.Queue(["Typeset", MathJax.Hub, "mathDiv"]);
  });

</script>

原始版本在我的系统上运行了2623,2247毫秒,因此(不是)仅与另一台计算机有关。

处理大量操作时,请考虑以可管理的批次大小破坏这些操作。

答案 1 :(得分:2)

还有另一种加快速度的解决方案:使用System.Collections.ArraLyist

我对您的第三个解决方案做了一些修改:

Measure-Command -Expression {
  $count = 10000

  $myitems = New-Object System.Collections.ArrayList
  for ($x=0; $x -le $count; $x++) 
  {
    $myitems.add(@([pscustomobject]@{key=$x.path;ID="i'm a value"}))
  }
} | Select-Object -Property TotalMilliSeconds

在计算机上运行所有这些解决方案将显示以下结果:

TotalMilliseconds
-----------------
          35.9567
        2148.3292
        2408.9981
         151.4771

如您所见,它确实比使用普通的PowerShell ArrayList更快。

System.Collections.ArraLyist使用其大小根据需要动态增加的数组实现IList接口。

来源:https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.7.2