Question PowerShell et le framework dotnet 3.5

Plus d'informations
il y a 9 ans 8 mois #17881 par Laurent Dardenne
Pour info, si on utilise des scalaires cette classe est préférable à Compare-Object :
[code:1]
$base=1..10000
$final=10000..20000
measure-Command {
$set1 = New-Object System.Collections.Generic.HashSet`[object](,$base)
$set2 = New-Object System.Collections.Generic.HashSet`[object](,$final)
$set3 = New-Object System.Collections.Generic.HashSet`[object](,$base)
$set1.ExceptWith($final)
$set2.ExceptWith($base)
$set3.InterSectWith($final)
}
#TotalSeconds : 0,0061802

measure-Command {
$result=Compare-Object -ReferenceObject $base -DifferenceObject $final -include
}
#TotalSeconds : 45,4109085

$base=1..10000|% {$_.tostring()}
$final=10000..20000|% {$_.tostring()}
measure-Command {
$set1 = New-Object System.Collections.Generic.HashSet`[object](,$base)
$set2 = New-Object System.Collections.Generic.HashSet`[object](,$final)
$set3 = New-Object System.Collections.Generic.HashSet`[object](,$base)
$set1.ExceptWith($final)
$set2.ExceptWith($base)
$set3.InterSectWith($final)
}
#TotalSeconds : 0,0060648

measure-Command {
$result=Compare-Object -ReferenceObject $base -DifferenceObject $final
}
#TotalSeconds : 44,9446043
[/code:1]
On peut mélanger les éléments du tableaux, mais sans impact majeur :
[code:1]
Function Shuffle($list)
{
#from : stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp
$rng = new-Object System.Random
$n = $list.Count
while ($n -gt 1) {
$n--
$k = $rng.Next($n + 1)
$value = $list[$k]
$list[$k] = $list[$n];
$list[$n] = $value;
}
}

$base=1..10000
Shuffle $base
$final=10000..20000
Shuffle $final
measure-Command {
$set1 = New-Object System.Collections.Generic.HashSet`[object](,$base)
$set2 = New-Object System.Collections.Generic.HashSet`[object](,$final)
$set3 = New-Object System.Collections.Generic.HashSet`[object](,$base)
$set1.ExceptWith($final)
$set2.ExceptWith($base)
$set3.InterSectWith($final)
}
#TotalSeconds : 0,0067118

measure-Command {
$result=Compare-Object -ReferenceObject $base -DifferenceObject $final
}
#TotalSeconds : 45,5292967
[/code:1]

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 9 ans 6 mois #18237 par Arnaud Petitjean
Whooaa !!! Merci Laurent !

En particulier pour la classe HashSet que je ne connaissais
pas !!! :silly: :silly: :silly:

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 9 ans 2 mois #18978 par Laurent Dardenne
Les traitements sont par défaut sensible à la casse.
Un exemple pour des traitements insensible à la casse :
[code:1]
$CodeCS=@'
using System;
using System.Collections.Generic;

public class MyStringComparer : EqualityComparer<string>
{
public override bool Equals(string s1, string s2)
{
return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase);
}

public override int GetHashCode(string s)
{
return base.GetHashCode();
}
}
'@
Add-type -TypeDefinition $codecs


[string[]] $Names=@('un','deux','trois','quatre','Cinq','Six')
[string[]] $Keys=@('Deux','Quatre','Six')

$Set = New-Object System.Collections.Generic.HashSet[String](,$Names)
$Set.IntersectWith($Keys)
$Set
#Six : une seule correspondance -> case sensitive

$MyComparer=New-Object MyStringComparer
$Set = New-Object System.Collections.Generic.HashSet[String] -argumentlist $Names,$MyComparer
$Set.IntersectWith($Keys)
$Set
# deux
# quatre
# Six
#Toutes les correspondances -> case insensitive
[/code:1]

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 7 ans 8 mois #22133 par Laurent Dardenne
Un article sur les performance entre certains cmdlets et une hashset.

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.079 secondes
Propulsé par Kunena