View Single Post
Old 08-31-2006   #2 (permalink)
Jouko Kynsijärvi


 
 

Re: Testing object arrays using Compare-Object and -contains

Alex K. Angelopoulos [MVP] wrote:
> I'm having trouble finding a simple scheme for getting all objects
> which are members of a set B but not also members of another set A.


> I also checked into using Compare-Object. I like the way it works in
> terms of the thinking behind it, but it takes some low-level unboxing
> to work right. The shorthand is pretty lengthy:
>
> diff (gsnp) (gsnp -r) | ?{$_.SideIndicator -eq "=>"} | %{
> $_.InputObject} | asnp


Here is another approach:

$a=@(gsnp | ?{ !$_.isdefault }); $a+=@(gsnp -r)
$a | group name | ?{ $_.count -eq 1} | asnp

> Compare-Object also takes a while to run; this is not really the kind
> of thing it was specifically designed to do.


In my testing Get-PSSnapin without -r is the slow one:
[~] (measure-command { get-pssnapin }).milliseconds
714
[~] (measure-command { get-pssnapin -r}).milliseconds
7

....Compare-Object on the other hand is quite fast:
[~] $a = 1..500; $b = 5..505
[~] (measure-command { compare-object $a $b }).milliseconds
61


My System SpecsSystem Spec