I have been working on refactoring a large script, with an eye on improved performance, and I find that quiet often a .NET approach will be faster, and sometimes MASSIVELY faster, than the PowerShell cmdlet equivalent. So I have started testing to see where I should use .ENT and where I needn't bother. The pattern I THOUGHT I was seeing was when large numbers of items or iterations are needed, .NET is faster, which makes a lot of sense.
But today I found myself needing to delete a folder with large numbers of subfolders and items in those subfolder. Some crap that the new Autodesk installers leave behind. The single C:\Autodesk\WI folder can easily be 10GB and tons of subfolders and files for a single Autodesk program that was just installed, and balloons to 5 times that as multiple programs are installed in sequence. So, seems like a good chance for .NET to be faster. SO I duplicated the folder and tried this...
(Measure-Command {
Remove-Item 'C:\Autodesk\WI - 1' -Recurse -Force -ErrorAction Stop
}).TotalSeconds
(Measure-Command {
[System.IO.Directory]::Delete('C:\Autodesk\WI - 2', $True)
}).TotalSeconds
Remove-Item took .037 seconds. And [System.IO.Directory]::Delete() took .045. So my first question is...
Is there a resource, or information in the Microsoft documentation, that enumerates which cmdlets are highly optimized and which are better to replace with a .NET alternative?
And, are there any gotchas with regards to testing procedures? One I already have seen is that sometimes the first test is slow, so usually in an A/B test like this I would make 4 test folders to delete, then test A/B/A/B, to verify that a slow first A isn't due to being the first code to run. But because the folder here is so large I didn't bother initially, and in fact got the reverse result.
And finally, I wonder if there are any gotchas to be aware of with using .NET in PowerShell in general? Certainly it means making more extensive use of Try/Catch/Finally since you loosing the typical SilentlyContinue behavior of PowerShell, but anything else?
EDIT: To try to remove ANY potential for "opinion" from the question I am rephrasing the questions so no one can be confused. So. 1: IS there a resource that discusses what cmdlets have been optimized for performance vis-a-vis their .NET equivalents? (True or False)
2: IF there is such a resource, where can it be found? (Link)
3: Regarding performance profiling, IS THERE a canonical testing procedure that returns consistent results? (True or False)
4: IF there is, what is it (Link or Code).
5: Regarding using .NET calls in Powershell, in lieu of native PowerShell cmdlets, is there an argument against doing so, or cautions to be aware of? (Link, because no one here is an expert so only a link to another source seems to be "not an opinion")
Please NOTE, if you didn't understand the questions until this revision, please DO NOT ANSWER. You aren't qualified, and I don't care what you have to say. If, on the other hand, you have a big brain and could see the whole point of the initial questions, then please DO answer the new questions, citing your sources and keeping your (expert) opinion out of it, because god forbid a professional resource like Stack Overflow should stoop to professional, expert opinion.