how to profile(timing) in powershell

本秂侑毒 提交于 2020-01-02 13:27:51

问题


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


回答1:


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.




回答2:


Posting your script here would really help in giving an accurate answer.

You can use Measure-Command to see how much time each statement in your script is taking. However, you have to wrap each statement in Measure-Command.

Trace-Command can also be used to trace what is happening when the script runs. The output from this cmdlet can be quite verbose.

http://www.jonathanmedd.net/2010/06/powershell-2-0-one-cmdlet-at-a-time-104-trace-command.html




回答3:


Here's a recent blog about speeding up for loops that shows you how to build a "test harness" for timing loops:

http://www.dougfinke.com/blog/index.php/2011/01/16/make-your-powershell-for-loops-4x-faster/




回答4:


A quick and simple poor-man's profiler is simply to step through the code in the ISE debugger. You can sometimes feel how slow a part of the code is just by stepping over it or by running to some breakpoint.



来源:https://stackoverflow.com/questions/4780305/how-to-profiletiming-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!