I know the C# element of this is OT here but I am at a loss and am hopeful someone with more powershell experience than I may know something.
I have tried several ways to loop through some code with powershell invoked from with C# but each invocation runs painfully slow and degrades very quickly to almost a halt.
If I create and dispose of the System.Management.Automation PowerShell instance for each iteration the speed maintains its pace for slightly longer but degrades as well.
Ultimately the speed of a single invocation is dreadful.
My use case is calling a function and sending in a some strings and dictionaries of lists as parameters. I need to do this a few million times.
The C# app queries a database for input and writes back processed data. If not invoking powershell that process alone is extremely fast.
A thought would be to never leave the ps execution environment and expose some C# classes to the ps environment that yield input and consume output.
The loop would run within a single ps invocation. If possible, that class that provides data utilizes a generator, so I certainly would not want to consume its dataset before iterating as that would consume far too much memory. I could create some code that accepts this properly via a pipeline.
What are any thoughts from people who have been down this road?