0

I have a huge array of XML nodes, sometimes up to 400k items. I use where-object to to select items based on Type attribute. My problem is that it takes quite some time to walk over these items and it would be easier if I could remove items I have just selected from array, so that array would be smaller for next object. Here's what I'm doing:

$subset = $items | Where-Object {$_.Type -eq "2"}
'prosess ItemType 2'

$subset = $items | Where-Object {$_.Type -eq "4"}
'prosess ItemType 4'

$subset = $items | Where-Object {$_.Type -eq "8"}
'prosess ItemType 8'

Problem is that every time I have to iterate over $items again, instead of iterating over what is remaining from previous where-object

It also doesn't help that where-object is a single thread operation and is executed only on one core

What I was thinking about is something like this, but I'm not sure if it will perform better:

$subset = $items | Where-Object {$_.Type -eq "2"}
$items = $items | Where-Object {$_.Type -ne "2"}
'prosess ItemType 2'

$subset = $items | Where-Object {$_.Type -eq "4"}
$items = $items | Where-Object {$_.Type -ne "4"}
'prosess ItemType 4'

$subset = $items | Where-Object {$_.Type -eq "8"}
'prosess ItemType 8'
1
  • Have you considered using a linked-list instead of an array? May be more efficient considering the amount of elements is approaching 400k. Easier to remove elements from the middle.techrepublic.com/article/… Commented Oct 19, 2012 at 12:23

1 Answer 1

2

Have you tried the Select-Xml cmdlet or the XmlDocument.SelectNodes method? Querying XML via its native methods(using XPath), should perform much faster than Where-Object.

Sign up to request clarification or add additional context in comments.

3 Comments

It is a very good idea, I will try it and report how it goes, thanks
You were right it takes just 15 seconds with SelectNodes vs 2.5 minutes with Where-Object
Worth every millisecond of it ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.