how to profile(timing) in powershell

Question:

My powershell script runs slowly, is there any way to profile the powershell script?

Answer:

You can do random-pausing in the Powershell debugger. Get the script running, and while it’s running, type Ctrl-C. It will halt and then you can display the stack. That will tell you where it is, what it’s doing, and why. Do this several times, not just once.

Suppose it is taking twice as long as it could. That means each time you interrupt it the probability you will catch it doing the slow thing is 50%. So if you interrupt it 10 times, you should see that on about 5 samples.

Suppose it is taking 5 times as long as it could. That means 4/5 of the time is being wasted, so you should see it about 8 times out of 10.

Even if as little as 1/5 of the time is being wasted, you should see it about 2 times out of 10. Anything you see on as few as 2 samples, if you can find a faster way to do it, will give you a good speed improvement.

Source:

how to profile(timing) in powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply