Why PowerShell workflow is significantly slower than non-workflow script for XML file analysis

Question:

I am writing a PowerShell program to analyse the content of 1900+ big XML configuration files (50000+ lines, 1.5Mb).
Just for test I move 36 test files to my PC (Win 10; PS 5.1; 32GB RAM) and write quick script to test the speed of execution.

That is completed for 36 to 40 seconds. I done several tests with measure-command.

Then I tried workflow with foreach -paralell assuming that parallel loading of several files will give me more faster process.

Script with the workflow needs between 118 and 132 seconds.

Now I am just wondering what could be the reason that workflow works so much slower? Recompiling to XMAL maybe or slower algorithm for loading XML files in WWF?

Answer:

foreach -parallel is by far the slowest parallelization option you have with PowerShell, since Workflows are not designed for speed, but for long-running operations that can be safely interrupted and resumed.

The implementation of these safety mechanisms introduces some overhead, which is why your script is slower when run as a workflow.

If you want to optimize for execution speed, use runspaces instead:


Fast XML processing bonus tips:

As wOxxOm suggests, using Xml.Load() is way faster than using Get-Content to read in the XML document.

Furthermore, using dot notation ($xml.root.servers.server) and the Where({}) extension method is also going to be painfully slow if there are many servers or server nodes. Use the SelectNodes() method with an XPath expression to search for “Server1” instead (be aware that XPath is case-sensitive):

Source:

Why PowerShell workflow is significantly slower than non-workflow script for XML file analysis by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply