PowerShell inferior OS in hard loops

I previously came across a suggestion to call System.Threading.Thread.Sleep (0); in witchcraft loops in C # to prevent the processor from freezing and to use it effectively.

I have a PowerShell script that has a tight loop, and I am wondering if I should call [Thread] :: Sleep (0) or Start-Sleep 0 or will work for me from time to time.

+3
source share
2 answers

I find that there are a couple of problems with explicitly exiting the stream through .Sleep () or in other ways, when you just make sure that it will not capture the processor. Firstly, it just makes your code bad, as it sprinkles Thread.Sleep (0). You can comment on each instance, but it does not look great.

, , . , script , .

ThreadPriority BelowNormal Lowest. , , , , .

[Thread]::CurrentThread.ThreadPriority = System.Threading.ThreadPriority.Lowest
+7

Thread.SpinWait(20). Intel HT. , CPU Sleep (0) , SpitWait (20)

0

Source: https://habr.com/ru/post/1699490/


All Articles