具有初始容量的Powershell哈希表 [英] Powershell Hashtable with Initial Capacity

查看:67
本文介绍了具有初始容量的Powershell哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您如何在Powershell中以初始容量声明哈希表.我知道我希望它有多大,但我必须在循环中为其分配值.

I was wondering how you declare a hashtable in powershell with an initial capacity. I know how large I want it to be but I have to assign the values to it in a loop.

所以像这样:

$hashtable = @{} (100)

推荐答案

JeroenMostert 对于为什么您可能不需要指定初始容量:

JeroenMostert makes a good point as to why you may not need to specify an initial capacity:

请注意,尽管预先指定容量通常不会在内存或运行时方面为您带来多大好处;它已经实现了大幅度的动态调整大小,并且如果您猜对了,好处就会基本消失.

Be aware that pre-specifying the capacity usually doesn't win you much in terms of memory or runtime, though; it already implements generous dynamic sizing, and if your guess is off, the benefits basically evaporate.

如果确实需要指定初始容量:

PetSerAl 表示感谢.

Tip of the hat to PetSerAl for his help.

由于PowerShell的哈希表对于键查找总是不区分大小写,因此[hashtable]::new(100)不能 起作用,不幸的是,因为默认情况下会创建一个case- 敏感哈希表.

Because PowerShell's hashtables are always case-insensitive with respect to key lookups, [hashtable]::new(100) does not work, unfortunately, because the default is to create a case-sensitive hashtable.

因此,需要使用[System.Collections.Hashtable]构造函数重载,该重载允许指定键相等比较方法,以便您指定区分文化的,不区分大小写的相等比较器,以匹配PowerShell的 v6.1- [1] 中常见的哈希表行为:

Therefore, use of a [System.Collections.Hashtable] constructor overload that allows specifying the key-equality comparison method is required, so that you can specify a culture-sensitive, case-insensitive equality comparer to match PowerShell's usual hashtable behavior in v6.1-[1]:

# PSv5+ syntax
$hashtable = [hashtable]::new(100, [StringComparer]::CurrentCultureIgnoreCase)

# PSv4- syntax
$hashtable = New-Object hashtable 100, ([StringComparer]::CurrentCultureIgnoreCase)

PetSerAl提供以下选择:

PetSerAl offers the following alternative:

$hashtable = [System.Collections.Specialized.CollectionsUtil]::CreateCaseInsensitiveHashtable(100)

注意:在 v6.2 + 中,PowerShell 现在使用普通字符串比较器([StringComparer]::OrdinalIgnoreCase).

Note: In v6.2+ PowerShell now uses an ordinal string comparer ([StringComparer]::OrdinalIgnoreCase).

另外,正如PetSerAl指出的那样, PowerShell v6.1- 缓存当前会话的键相等比较器-因此,当前区域性的会话内更改为忽略 .如果要模拟这种可疑的行为,PetSerAl提供以下命令:

Additionally, as PetSerAl points out, PowerShell v6.1- caches the key-equality comparer for the current session - so an in-session change of the current culture is ignored. If you want to emulate this - questionable - behavior, PetSerAl provides the following command:

$hashtable = [hashtable]::new(100,
 [hashtable].GetProperty(
   'EqualityComparer',
   [System.Reflection.BindingFlags]'NonPublic, Instance'
 ).GetValue(@{}))

尽管使用反射来访问非公共属性,但是这种方法应该是安全的,因为目标属性的访问修饰符为protected,这意味着它具有合同"具有派生的公共类,并且不会消失.

Despite the use of reflection to access a non-public property, this approach should be safe, because the targeted property's access modifier is protected, which means it has a "contract" with derived public classes and won't go away.

请注意,优化哈希表的另一种方法是指定其负载因子,并且还存在用于指定该因子的构造函数重载.

Note that another way to optimize a hashtable is to specify its load factor, and there are constructor overloads for specifying that factor as well.

来自文档(添加了重点):

哈希表的容量用于根据负载因子计算哈希表存储桶的最佳数量.容量会根据需要自动增加.

A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. Capacity is automatically increased as required.

负载系数是元素与存储桶的最大比率.较小的负载因子意味着更快的查找速度,但会增加内存消耗.

The load factor is the maximum ratio of elements to buckets. A smaller load factor means faster lookup at the cost of increased memory consumption.

当实际负载系数达到指定的负载系数时,铲斗数会自动增加到最小素数,该最小素数大于当前铲斗数的两倍.

When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.


[1]注意,在许多情况下,PowerShell使用不变文化进行字符串操作,但哈希表似乎是一个例外-请参见此答案.
strong> v6.1-源代码揭示了CurrentCultureIgnoreCase在PowerShell哈希表构造函数中的用法,以及


[1] Note that in many contexts PowerShell uses the invariant culture for string operations, but hashtables seem to be an exception - see this GitHub issue and this answer.
The v6.1- source code reveals the use of CurrentCultureIgnoreCase in PowerShell's hashtable constructor, and the v6.2+ source code now shows use of ordinal (case-insensitive) comparison.

这篇关于具有初始容量的Powershell哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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