PSObject、Hashtable 和 PSCustomObject 的区别 [英] Difference between PSObject, Hashtable, and PSCustomObject

查看:31
本文介绍了PSObject、Hashtable 和 PSCustomObject 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能详细解释一下?如果我使用

Can anybody explain the details? If I create an object using

$var = [PSObject]@{a=1;b=2;c=3}

然后我使用 getType() 查找它的类型,PowerShell 告诉我它的类型是 Hashtable.

and then I look for its type using getType() PowerShell tells me it's of type Hashtable.

当使用 Get-Member(别名 gm) 来检查对象,很明显已经创建了一个哈希表,因为它有一个 keys 和一个 values 属性.那么与普通"哈希表有什么区别?

When using Get-Member (alias gm) to inspect the object it's obvious that a hashtable has been created, since it has a keys and a values property. So what's the difference to a "normal" hashtable?

另外,使用 PSCustomObject 有什么好处?当使用这样的东西创建一个

Also, what's the advantage of using a PSCustomObject? When creating one using something like this

$var = [PSCustomObject]@{a=1;b=2;c=3}

对我来说唯一明显的区别是 PSCustomObject 的不同数据类型.与键和值属性不同的是,使用 gm 进行检查显示,现在每个键都已添加为 NoteProperty 对象.

the only visible difference to me is the different datatype of PSCustomObject. Also instead of keys and value properties, a inspection with gm shows that now every key has been added as a NoteProperty object.

但是我有什么优势呢?我可以使用它的键来访问我的值,就像在哈希表中一样.我可以在 PSCustomObject 中存储多个简单的键值对(例如键对象对),就像在哈希表中一样.那么有什么好处呢?有什么重要的区别吗?

But what advantages do I have? I'm able to access my values by using its keys, just like in the hashtable. I can store more than simple key-value pairs (key-object pairs for example) in the PSCustomObject, JUST as in the hashtable. So what's the advantage? Are there any important differences?

推荐答案

我认为您将看到的最大区别是性能.看看这篇博文:

I think the biggest difference you'll see is the performance. Have a look at this blog post:

有效组合对象 - 使用哈希表索引对象集合

作者运行如下代码:

$numberofobjects = 1000

$objects = (0..$numberofobjects) |% {
    New-Object psobject -Property @{'Name'="object$_";'Path'="Path$_"}
}
$lookupobjects = (0..$numberofobjects) | % {
    New-Object psobject -Property @{'Path'="Path$_";'Share'="Share$_"}
}

$method1 = {
    foreach ($object in $objects) {
        $object | Add-Member NoteProperty -Name Share -Value ($lookupobjects | ?{$_.Path -eq $object.Path} | select -First 1 -ExpandProperty share)
    }
}
Measure-Command $method1 | select totalseconds

$objects = (0..$numberofobjects) | % {
    New-Object psobject -Property @{'Name'="object$_";'Path'="Path$_"}
}
$lookupobjects = (0..$numberofobjects) | % {
    New-Object psobject -Property @{'Path'="Path$_";'Share'="Share$_"}
}

$method2 = {
    $hash = @{}
    foreach ($obj in $lookupobjects) {
        $hash.($obj.Path) = $obj.share
    }
    foreach ($object in $objects) {
        $object |Add-Member NoteProperty -Name Share -Value ($hash.($object.path)).share
    }
}
Measure-Command $method2 | select totalseconds

博客作者的输出:

TotalSeconds
------------
 167.8825285
   0.7459279

他对代码结果的评论是:

His comment regarding the code results is:

当你把它们放在一起时,你可以看到速度的差异.在我的计算机上,对象方法需要 167 秒,而哈希表方法将需要不到一秒的时间来构建哈希表,然后进行查找.

You can see the difference in speed when you put it all together. The object method takes 167 seconds on my computer while the hash table method will take under a second to build the hash table and then do the lookup.

以下是其他一些更微妙的好处:PowerShell 3.0 中自定义对象默认显示

Here are some of the other, more-subtle benefits: Custom objects default display in PowerShell 3.0

这篇关于PSObject、Hashtable 和 PSCustomObject 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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