I have two arrays that I would like to take the difference between. I had some success with COMPARE-OBJECT, but is too slow for larger arrays. In this example $ALLVALUES and $ODD are my two arrays.
I used to be able to do this efficiently using FINDSTR ex. FINDSTR /V /G:ODD.txt ALLVALUES.txt > EVEN.txt FINDSTR finished this in under 2 seconds for 110,000 elements. (even had to read and write from the disk)
I'm trying to get back to the FINDSTR performance where it would give me everything in ALLVALUES.txt that did NOT match ODD.txt (giving me the EVEN values in this case)
NOTE: This question is not about ODD or EVEN, only a practical example that can be quickly and visually verified that it is working as desired.
Here is the code that I have been playing with. Using COMPARE-OBJECT, 100,000 took like 200 seconds vs 2 seconds for FINDSTR on my computer. I'm thinking there is a much more elegant way to do this in PowerShell. Thanks for your help.
# ------- Build the MAIN array
$MIN = 1
$MAX = 100000
$PREFIX = "AA"
$ALLVALUES = while ($MIN -le $MAX)
{
"$PREFIX{0:D6}" -f $MIN++
}
# ------- Build the ODD values from the MAIN array
$MIN = 1
$MAX = 100000
$PREFIX = "AA"
$ODD = while ($MIN -le $MAX)
{
If ($MIN%2) {
"$PREFIX{0:D6}" -f $MIN++
}
ELSE {
$MIN++
}
}
Measure-Command{$EVEN = Compare-Object -DifferenceObject $ODD -ReferenceObject $ALLVALUES -PassThru}